Search code examples
javascriptjqueryiframepostmessage

how to use postmessage in an iframe to redirect the user to a page on the main document


I have a page with an iframe on it, and I need a button inside the frame to do a redirection to another page, if I do it the normal way the content of the iframe will be reloaded, what I need is the main window to change its url.

I havent posted any code below as I dont know if this is even possible


Solution

  • Yes this is possible. You have to send a message from the iFrame, when parent receives the message, then you do something. Here is an example: http://jsbin.com/necuvi/2/. Here is some documentation on how it works. https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage