最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing parent window elements from iframe - Stack Overflow

programmeradmin3浏览0评论
  • 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.
  • 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.
Share Improve this question edited Feb 20, 2013 at 5:04 Bhushan Firake 9,4585 gold badges46 silver badges79 bronze badges asked Feb 20, 2013 at 4:59 RachelRachel 1,15911 gold badges26 silver badges43 bronze badges 1
  • you have 500 correct answers, pick one – I wrestled a bear once. Commented May 4, 2014 at 16:47
Add a comment  | 

4 Answers 4

Reset to default 9
window.parent.document.getElementById('target'); 

both resources should be on same origin

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 - e.g. 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?

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'); 

     }
}
发布评论

评论列表(0)

  1. 暂无评论