简体   繁体   中英

Accessing parent window elements from iframe

  • I have a customer.jsp page with an iframe .
  • iframe has a button. on click of the button, i need to access a dialog box which is inside customer.jsp page.
  • I tried window.parent.document.getElementById('formDialog'); but am getting null value.
window.parent.document.getElementById('target'); 

两种资源都应该是同一个来源

Communication between an iframe and parent document is not possible for cross-origin resources. It will only work if the iframe and the containing page are from the same host, port and protocol - eg http://example.com:80/1.html and http://example.com:80/2.html

 Assuming both resources are from the same origin

In the iframe, window.parent refers to the global object of the parent document, not the document object itself. I believe you would need to use parent.document.getElementById('formDialog')

You can access parent window elements through parent.document.getElementById('formDialog');

If you get null are you able to get that element within the context of that Iframes parent? Are you referencing the right ID and the right parent?

seems like you forgot something in your code. try this:

window.parent.window.document.getElementById('formDialog'); 

Try this. Hope this helps. Let's say I have CallParentFunction from button click of a button in an iframe.

function CallParentFunction(){
     if (top && top.opener && top.opener.top) {
        top.opener.document.getElementById('formDialog'); 
     }
      else {
        top.document.getElementById('formDialog'); 

     }
}

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