Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup window accessing parent dom

I have a popup window that needs to access the parent dom to generate a print page. The structure of the print page is significantly different then the structure of the parent so a print css would not solve the problem. I basically want to popup a window and then have that window grab some data from the parent of even access the dom from the popup and generate the print page without having to go to the server again. Any ideas how i can achieve this?

Im using the standard

window.open()

to pop up a window. I need this solution to not be a hack and be cross browser compatible with all major browsers.

Thanks in advance!

like image 388
Scoota P Avatar asked Nov 09 '12 16:11

Scoota P


People also ask

How to access Popup parent window element with JavaScript?

open() will return you a handle to the new window. var popUpHandle = window. open(); With this handle you should be able to access the DOM of the PopUp.

How do you find the parent element in a child window?

You just need to prefix “ window. opener. ” and write the same code that you will write in the parent window's HTML page to access its elements.


2 Answers

Sajjan's answer is a start, but better make sure your objects are available before you try to access them:

var opener = window.opener;
if(opener) {
    var oDom = opener.document;
    var elem = oDom.getElementById("your element");
    if (elem) {
        var val = elem.value;
    }
}

Otherwise, you do run the risk that the opener doesn't respond to your initial call, and that you can't get the element from it.

As jQuery, I think (based on an answer, here: how to access parent window object using jquery?):

var opener = window.opener;
if(opener) {
    var elem = opener.$("#elementId");
    if (elem) {
        var val = elem.val(); // I forgot we're dealing with a jQuery obj at this point
    }
}
like image 94
Jason M. Batchelor Avatar answered Sep 28 '22 16:09

Jason M. Batchelor


window.opener.document.getElementById("your element").value

like image 30
Sajjan Sarkar Avatar answered Sep 28 '22 17:09

Sajjan Sarkar