JS frame 跨域 传值

1.在index.html 页面定义一个 函数用于接收 子页面的调用。

<iframe id="common_iframe" class="common_content" style="height:100%;100%;border:0 none;" src='index2.html'></iframe>

function reFun(data) {
  alert(data);
}

2.在index2.html 页面中 增加一个的iframe ,但是调用地址 需要与index.html 同一个域(可以根据需要 动态创建iframe 灵活调用)。

这样就可以在 index2.html 中调用 index.html 的函数了。

window.parent.parent.reFun("goMain"); // 调用父 窗体函数。

-------------------------------------------------

3. 使用HTML postMessage 进行消息传递

// 子页面像主页面 传递消息

window.parent.parent.reFun('goMain');

// 主页面 消息监听

window.onmessage = function (e) {
if (e.origin == "http://www.XXX.com") {
if (e.data == "goOrder") {
openSection("myOrderSection");
}
}
}

// 主页面向子页面发送消息

window.frames[0].postMessage('123','*');

//子页面接收同主页面一样
原文地址:https://www.cnblogs.com/90nice/p/4657075.html