Question

I have a browser bookmarklet which loads an iframe on the page. I then need that iframe to be able to pass a string back to the parent window. The child page has the following function:

function post(m){
parent.postMessage(m,'*');
}

This works fine as long as the child page (the one loaded in the iframe) is on http. But when it's https the postMessage doesn't work. I have not been able to find any documentation of this, or any way to get around it.

Any ideas?

EDIT:

This happens both on FF and Chrome (haven't tried IE). To clarify: the iframe DOES load, it's just that the postMessage doesn't get executed. On an HTTP page everything works fine. The code I'm using to receive the string is as follows:

function listener(event){
if ( event.origin !== "http://www.mysite.com" )
return

if (event.data=="string1"){function1();}
if (event.data=="string2"){function2();}
}

if (window.addEventListener){
addEventListener("message", listener, false)
} else {
attachEvent("onmessage", listener)
}
Était-ce utile?

La solution

I think you need your morning coffee.

Here is your fix:

if ( event.origin.replace('https:', 'http:') !== 'http://www.mysite.com'  ) return;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top