Heim > Web-Frontend > js-Tutorial > Hauptteil

jQuery implementiert die domänenübergreifende Iframe-Schnittstellenmethodecalling_jquery

WBOY
Freigeben: 2016-05-16 16:09:32
Original
1118 Leute haben es durchsucht

cross.js

复制代码 代码如下:

(Funktion(global){
  global.Cross = {
    signalHandler: {},
    on: function(signal, func){
      this.signalHandler[signal] = func;
    },
    Aufruf: 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,
        Protokoll;
    versuche es mit {
        Protokoll = 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)){
          Löschen Sie Cross.signalHandler[protocol.signal];
        }
    } Catch (e) {
      console.log(e);
      throw new Error("cross error.");
    }
  });
})(Fenster);

a.html

复制代码 代码如下:



 
   
   
    <script><br>       Funktion call_b(){<br>         var ifw = $("#ifr")[0].contentWindow;<br>         //调用iframe子页面的公开的test接口, 子页面域名为<a href="http://localhost:8088">http://localhost:8088</a><br>         Cross.call(ifw,"<a href="http://localhost:8088%22,%22test%22,%7Bt">http://localhost:8088","test",{t</a>: $("#txt").val()});<br>       }<br>     </script>