Cross window scripting

How can I refer to a window by name only?

i.e. :

The main window of the site is called "mainwindow".

When I open a popup from mainwindow called "popup1" I can refer to "mainwindow" using:


When I open a popup from "popup1" called "popup2", how can I refer to "mainwindow" from "popup2"?
Who is Participating?
Cyber-DrugsConnect With a Mentor Commented:
Can't say I have ever had to do this, but here's something you could try, it's a shot in the dark....

Option 1:
Determine the name of the parent window
Determine the name of that determined name
loop through this until you find an absolute parent

Option 2:
Main window opens newpage as: mypage.php?parent=mainwindow
this alters the javascript of that open popup to include the parent name
keep passing the parent name up so that when it's needed to be determined, no calculations are required

Option 2 should work (if you have server side scripting on your webpages)
Option 1 I'm not sure will work or not, as I tend to avoid popups where I can, and instead use floating <div> elements
Just to clarify, are you trying to determine the origination of "popup2", via code? Eg:

under_dogAuthor Commented:
Basically yes ...

One of the things I need to do is redirect the user to another page in MainWindow based on an onclick action in popup2.

From Popup1 I can simply do:

window.opener.location = 'redirecthere.php';

so my question is how can I redirect MainWindow from Popup2? Once I have that working I can adapt it for my other needs :p
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

under_dogAuthor Commented:
Oh I think you misunderstand ... I _know_ the name of the window I want to redirect ... I'm just not sure of the syntax to reference it ... I've tried:


and everything else I could think of, but nothing works :p
Ah ok, try this:

function changeURL(winName, newURL) {
  win =, winName);
under_dogAuthor Commented:
hmm ok that does work ... but I also need to get the original href of the mainWindow BEFORE I redirect them ... using your idea gives me a handle to give the window focus and manipulate it later, which is great ... but I need to know what the original page was.

Apologies for the delay in getting back to you, but it's been hell at work and I've only just had some time to test :p
Ah ok, I have no clue how, and IF you can even reference a window to determine it's location...

But if you want to determine the location of the highest point, example:


and you want Popup3 to display the location of Window1, you can use this:


Does that help at all?
forrest321Connect With a Mentor Commented:
under_dogAuthor Commented:
Ok a combination of both of your suggestions and I have come up with something that works:

mainFound = false;
objectName = 'window';
while (mainFound == false) {
  objectName += '.opener'      
  objectNameTest = objectName + '.name';
  if (eval(objectNameTest) == "mainwindow") {
    mainFound = true;


windowHandle = eval(objectName);


I can then use the windowHandle variable to manipulate the top window, and it works regardless of how many other windows are in between!

Thanks for your help guys!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.