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

本文标签: