A Brief Introduction to implementing cross-domain iframe interface method calls, domain iframe

Source: Internet
Author: User

A Brief Introduction to implementing cross-domain iframe interface method calls, domain iframe

A Brief Introduction to cross-origin iframe interface method calling:

Page a.html domain name for www.a.com, embedded page http://www. B .com/ B .html.

B .htmlto call the js function in a.html, because the two pages are not in the same domain, the system will prompt that they are not authorized.

The following describes how to solve this problem. If you need it, you can refer to it.

I. The cross. js code is as follows:

(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);

The Code of 2.a.html is as follows:

<! DOCTYPE html> 

The code for "3. B .html" is as follows:

<! DOCTYPE html> 

Original address: http://www.51texiao.cn/jqueryjiaocheng/2015/0525/2453.html

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 17186.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.