Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 508
Rachel Avatar asked Feb 20 '13 04:02

Rachel


People also ask

How to call a parent window function from an iframe?

JavascriptWeb DevelopmentFront End Technology. To call a parent window function, use “window.top”. You can try to run the following code to call a parent window function from an iframe.

How to get the source of an iframe using JavaScript?

As the iframe got an ID named 'source'. So, with javascript, it's simple to get the element. var destination = document.getElementById ('destination'); But as we are targetting the content inside the iframe, we need to address the contentwindow inside the iframe. So the code will be like below -

Is it possible to cross-origin communication between an iFrame and parent website?

Cross-origin communication in between iframe and it’s parent website is not any hack or something, but with simple functions you can make it happen Yes, it's not any hack or something, but with simple functions you can communicate in between iframe and it's parent website.

How to send a message from an iframe?

To send a message using window.postMessage method: you need to add event listeners in the parent to receive messages from iframe Note: this method can be used in same-domain iframe also.


2 Answers

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

both resources should be on same origin

like image 151
Talha Avatar answered Oct 17 '22 15:10

Talha


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

like image 43
Bhushan Firake Avatar answered Oct 17 '22 17:10

Bhushan Firake