简体   繁体   中英

How can I pass a variable from iframe to a parent window?

I have a colorbox parent page which contains a TabContainer with different iFrames on the case for the second tab in which a change is made to the database and need to update the a textbox of the parent window.

在此处输入图片说明

Unfortunately you can't directly interact across the boundaries of an iframe as this would present a security risk.

One workaround can be to alter the url within the iframe to include an #anchor value or ?querystring and then read the iframe's url from the parent.

I hope you have tried postMessage , this is a javascript API , but there is browser limitation.

Parent page can post message like this.

var win = document.getElementById("iframe").contentWindow

win.postMessage(
  "value",
  "iframe-domain"
);

And iframe page can listen to message like this.

<script>
function listener(event){
  if ( event.origin !== "[parent-page-domain]" )
    return;

 // operate with event.data
}

if (window.addEventListener){
  addEventListener("message", listener, false)
} else {
  attachEvent("onmessage", listener)
}
</script>

SOURCE - http://javascript.info/tutorial/cross-window-messaging-with-postmessage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM