user1152440
user1152440

Reputation: 905

document.getElementById on another window

Is it possible to get the id of the previous parent window if you launched the child window with document.location.href="diffPage.htm?name="+string; where string would change each time?

Basically I have the main window with an id=favouritesTab, called main.html that goes to diffPage.htm when a function is invoked.

I have tried window.parent.document.getelementById() such as

var favouritesScreen = window.parent.document.getElementById('favouritesTab');
if(favouritesScreen.style.display == 'inherit')
{
    ..so on

But it doesn't seem to work at all. Does anyone know if there is a solution to this?

Upvotes: 0

Views: 7085

Answers (1)

Rob W
Rob W

Reputation: 348972

Provided that your page is hosted at the same domain, accessing a document in the original window is possible via the window.opener property.

A full demo is here: http://jsfiddle.net/eAjqX/1/show/

  1. Click on the button to open a new window.
  2. Upon click in the new window, you will see that the body's content changes to the value of the input field.
  3. Navigate away using the provided link, in the original page.
  4. Repeat step 2. You will see that window.opener.document.getElementById still works.

The code for this demo (jQuery is not necessary):

// New window page:
var i = 0;
document.onclick = function (){
    try {
        alert(window.opener.document.getElementById('test').value);
    } catch (e) {
        alert(e);
    }
};
// Launcher
window.open("http://jsfiddle.net/eAjqX/show");

Upvotes: 1

Related Questions