时间:2021-07-01 10:21:17 帮助过:4人阅读
<!DOCTYPE html><html><head><title>iframe子页面与父页面通信(同域)</title><meta charset="utf-8"><script type="text/javascript">
function sayHello(){
alert("iframeCommunication1.html");
} function say(){
child1.window.sayHello();
child1.window.document.getElementById("button_c").value = "the call is complete";
}</script></head><body><h1>iframe子页面与父页面同域通信</h1>调用子页面child1.html中的sayHello()函数:<input type="button" id="button_p" name="hello" value="call" onclick="say()"><br><br><iframe src="child1.html" id="child1"></iframe></body></html><!DOCTYPE html><html><head><title>iframe子页面与父页面通信(同域)</title><meta charset="utf-8"><script type="text/javascript">
function sayHello(){
alert("child.html");
} function say(){
parent.sayHello();
parent.window.document.getElementById("button_p").value = "the call is complete";
}</script></head><body><p>调用父页面iframeCommunication1.html中的sayHello()函数:</p><input type="button" id="button_c" name="hello" value="call" onclick="say()"></body></html>对于IE8+及现代浏览器,跨域通信主要使用了html5给出的postMessage API来实现域间通信。postMessage的功能是允许程序员跨域在两个窗口/frames间发送数据信息。基本上,它就像是跨域的AJAX,但不是浏览器跟服务器之间交互,而是在两个客户端之间通信。
对于旧版浏览器,messenger.js使用了navigator对象在父窗口和iframe之间是共享的特性,在navigator对象上注册消息回调函数实现信息传递及共享。
下面给出一个很简单的例子:
<!DOCTYPE html><html><head><title>iframe子页面与父页面通信(跨域)</title><meta charset="utf-8"><script src="js/messenger.js"></script></head><body><h1>iframe子页面与父页面跨域通信</h1>向子页面child2.html发送信息:<input type="button" id="button_p" value="call" onclick="sendMessage('child2')"><br><br>获取信息:<pre id="output"></pre><br><br><iframe src="child2.html" id="child2" style="width: 50%; height: 300px;"></iframe><script type="text/javascript">
function say(msg){
var output = document.getElementById("output");
output.innerHTML = msg;
console.log("in parent");
} var messenger = new Messenger('parent', 'CROSS_DOMAIN_COMMUNICATION'),
child2 = document.getElementById('child2');
messenger.listen(say);
messenger.addTarget(child2.contentWindow, 'child2'); // 添加一个消息对象
function sendMessage(name) {
messenger.targets[name].send('message from parent');
}</script></body></html><!DOCTYPE html><html><head><title>iframe子页面与父页面通信(跨域)</title><meta charset="utf-8"><script src="js/messenger.js"></script></head><body><h1>child2</h1>向父页面iframeCommunication2.html发送信息:<br><input type="button" id="button_p" value="call" onclick="sendMessage('parent')"><br><br>获取信息:<pre id="output"></pre><br><br><script type="text/javascript">
function say(msg){
var output = document.getElementById("output");
output.innerHTML = msg;
console.log('in child2');
} var messenger = new Messenger('child2', 'CROSS_DOMAIN_COMMUNICATION');
messenger.listen(say);
messenger.addTarget(window.parent, 'parent'); // 添加一个消息对象
// messenger.addTarget(window.parent.frames[1], 'parent'); // 与父窗口的其他子窗口通信
function sendMessage(name) {
messenger.targets[name].send('message from child2');
}</script></body></html>[1] js之iframe子页面与父页面通信
[2] iframe跨域通信的通用解决方案-第二弹!(终极解决方案) | Tencent AlloyTeam
[3] Messengerjs项目主页i
以上就是iframe子、父页面域内及跨域通信实例的详细内容,更多请关注Gxl网其它相关文章!