Solved

Cross window scripting

Posted on 2007-03-27
9
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

730 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