您好,欢迎访问一九零五行业门户网

jQuery实现跨域iframe接口方法调用_jquery

cross.js
复制代码 代码如下:
(function(global){
  global.cross = {
    signalhandler: {},
    on: function(signal, func){
      this.signalhandler[signal] = func;
    },
    call: function(win, domain, signal, data, callbackfunc){
      var notice = {signal: signal, data: data};
      if(!!callbackfunc){
          notice[callback] = callback_ + new date().gettime();
          cross.on(notice[callback], callbackfunc);
      }
      var noticestr = json.stringify(notice);
      win.postmessage(noticestr, domain);
    }
  };
  $(window).on(message, function(e) {
    var realevent = e.originalevent,
        data = realevent.data,
        swin = realevent.source,
        origin = realevent.origin,
        protocol;
    try {
        protocol = json.parse(data);
        var result = global.cross.signalhandler[protocol.signal].call(null, protocol.data);
        if(!!protocol[callback]){
          cross.call(swin, origin, protocol[callback], {result: result});
        }
        if(/^callback_/.test(protocol.signal)){
          delete cross.signalhandler[protocol.signal];
        }
    } catch (e) {
      console.log(e);
      throw new error(cross error.);
    }
  });
})(window);
a.html
复制代码 代码如下:
call
    http://localhost:8088/b.html>>
b.html
复制代码 代码如下:
以上就是本文所述的iframe跨域的解决方案了,希望大家能够喜欢。
其它类似信息

推荐信息