Rumah >hujung hadapan web >tutorial js >jQuery melaksanakan kaedah antara muka iframe silang domain calling_jquery

jQuery melaksanakan kaedah antara muka iframe silang domain calling_jquery

WBOY
WBOYasal
2016-05-16 16:09:321173semak imbas

cross.js

复制代码 代码如下:

(fungsi(global){
  global.Salib = {
    pengendali isyarat: {},
    on: function(signal, func){
      this.signalHandler[signal] = func;
    },
    panggilan: fungsi(menang, domain, isyarat, data, panggilan balik func){
      var notice = {"signal": signal, "data": data};
      if(!!callbackfunc){
          notice["callback"] = "callback_" new Date().getTime();
          Cross.on(notis["callback"], callbackfunc);
      }
      var noticeStr = JSON.stringify(notis);
      win.postMessage(notisStr, domain);
    }
  };
  $(window).on("message", function(e) {
    var realEvent = e.originalEvent,
        data = realEvent.data,
        swin = realEvent.source,
        asal = realEvent.origin,
        protokol;
    cuba {
        protokol = JSON.parse(data);
        var result = global.Cross.signalHandler[protocol.signal].call(null, protocol.data);
        if(!!protokol["panggilan balik"]){
          Cross.call(swin, origin, protocol["callback"], {result: result});
        }
        if(/^callback_/.test(protocol.signal)){
          padamkan Cross.signalHandler[protocol.signal];
        }
    } tangkap (e) {
      console.log(e);
      buang Ralat baharu("ralat silang.");
    }
  });
})(tetingkap);

a.html

复制代码 代码如下:



 
   
   
   
      fungsi call_b(){
        var ifw = $("#ifr")[0].contentWindow;
        //调用iframe子页面的公开的test接口, 子页面域名为http://localhost:8088
        Cross.call(ifw,"http://localhost:8088","test",{t: $("#txt").val()});
      }