javascript window.top.postMessage or window.parent.postMessage not working after loading a new document in the child iFrame - St

I have a basic HTML page which includes an iFrame. When the parent loads and the iFrame within it, the

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

OK. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742267511a4412081.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信