Solved

Cross window scripting

Posted on 2007-03-27
9
280 Views
Last Modified: 2010-08-05
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:

window.opener

When I open a popup from "popup1" called "popup2", how can I refer to "mainwindow" from "popup2"?
0
Comment
Question by:under_dog
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:Cyber-Drugs
ID: 18799769
Just to clarify, are you trying to determine the origination of "popup2", via code? Eg:

MainWindow->Popup1->Popup2
0
 
LVL 9

Author Comment

by:under_dog
ID: 18799935
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
0
 
LVL 4

Accepted Solution

by:
Cyber-Drugs earned 300 total points
ID: 18800082
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
0
 
LVL 9

Author Comment

by:under_dog
ID: 18800155
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:

mainWindow.location
window.mainWindow.location
mainWindow.window.location

and everything else I could think of, but nothing works :p
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:Cyber-Drugs
ID: 18800302
Ah ok, try this:


function changeURL(winName, newURL) {
  win = window.open(newURL, winName);
}
0
 
LVL 9

Author Comment

by:under_dog
ID: 18808820
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
0
 
LVL 4

Expert Comment

by:Cyber-Drugs
ID: 18810816
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:

Window1->Popup1->Popup2->Popup3

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


alert(top.location);

Does that help at all?
0
 
LVL 2

Assisted Solution

by:forrest321
forrest321 earned 200 total points
ID: 18811909
window.opener.opener
0
 
LVL 9

Author Comment

by:under_dog
ID: 18815311
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!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now