admin管理员组文章数量:1332323
I have a basic HTML page which includes an iFrame. When the parent loads and the iFrame within it, the iFrame executes the following javascript:
myParent = window.top;
if (window.parent != window.top) {
myParent = window.parent;
}
dataString = '{"function":"$.SmartMenus.hideAll();"}';
myParent.postMessage(dataString, window.origin);
If a link inside the iFrame is clicked and loads a new document within the same iFrame which includes the exact same javascript code, the same code will not execute. Apparently window.top or window.parent are no longer in the correct scope.
I have tried window.opener but this does not work either. I am currently testing in FireFox 29.0.1 on Windows 7.
Any help is greatly appreciated!
UPDATE I should have mentioned that this is a cross domain request. However the page that is being loaded inside the iFrame has the same domain as the initial page that was loaded into the iFrame.
When testing in Chrome, I do see that I get the following error: Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http:/secondDomain:51990') does not match the recipient window's origin.
Why is it not ok to post a message from the same domain inside the iFrame? It was my understanding that the parent can listen for postMessages from any domain and it is up to the developer to choose to act on the message based on the origin of the domain that was making the request.
I have a basic HTML page which includes an iFrame. When the parent loads and the iFrame within it, the iFrame executes the following javascript:
myParent = window.top;
if (window.parent != window.top) {
myParent = window.parent;
}
dataString = '{"function":"$.SmartMenus.hideAll();"}';
myParent.postMessage(dataString, window.origin);
If a link inside the iFrame is clicked and loads a new document within the same iFrame which includes the exact same javascript code, the same code will not execute. Apparently window.top or window.parent are no longer in the correct scope.
I have tried window.opener but this does not work either. I am currently testing in FireFox 29.0.1 on Windows 7.
Any help is greatly appreciated!
UPDATE I should have mentioned that this is a cross domain request. However the page that is being loaded inside the iFrame has the same domain as the initial page that was loaded into the iFrame.
When testing in Chrome, I do see that I get the following error: Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http:/secondDomain:51990') does not match the recipient window's origin.
Why is it not ok to post a message from the same domain inside the iFrame? It was my understanding that the parent can listen for postMessages from any domain and it is up to the developer to choose to act on the message based on the origin of the domain that was making the request.
Share Improve this question edited Jun 10, 2014 at 23:47 Julian Dormon asked Jun 10, 2014 at 22:57 Julian DormonJulian Dormon 1,7795 gold badges33 silver badges63 bronze badges1 Answer
Reset to default 3OK. The problem had nothing to do with the scope of the window object. The problem was that the origin was passing to the parent no longer matched the parent's domain. It was an error in the way I constructed the origin domain. I opted for the '*' wild card and am filtering requests by domain on the parent's side when a message received.
本文标签:
版权声明:本文标题:javascript window.top.postMessage or window.parent.postMessage not working after loading a new document in the child iFrame - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742267510a2443687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论