Javascript - Targeting a window by name from a function?

onyxmoron

New Member
Messages
37
Reaction score
0
Points
0
I have a page with a link that opens a popup window. There's a link in the popup window that runs a javascript function that's supposed to change the url of the window that opened it. Because of the way the pages are set up I have to reference that original window by its name rather than by window.opener, etc. The window is named "originalwindow". I can reference it from a regular link in the popup like this:

Code:
<a href="http://www.google.com" target="originalwindow">
This works correctly. But I need to do it from a javascript function. I assumed something like this would work:

Code:
originalwindow.location.href = 'http://www.google.com';
but it doesn't do anything. I've tried every variation I can think of but can't get one that works, and I've been searching and searching on google and can't find anything that explains how to reference a window by its name in this way. What is the correct way to do this? I hope this makes sense.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
The name doesn't matter if it is from the popup window. Your best bet is to stick with window.opener. But it would be originalwindow.document.location.href as the name (if it worked, which it shouldn't) would be a reference to the Window object, not it's document.
 

onyxmoron

New Member
Messages
37
Reaction score
0
Points
0
The name doesn't matter if it is from the popup window.

Why doesn't the name matter?

Your best bet is to stick with window.opener.

That would be easiest, but as I said I can't use window.opener in this case. The popup is launched by a link in a frameset and the link in the popup window loads a new frameset into the original window. Once that happens the opener window isn't there any more so the links don't reference anything. So I have to reference the window by its name or in some other way.

But it would be originalwindow.document.location.href as the name (if it worked, which it shouldn't) would be a reference to the Window object, not it's document.

Why wouldn't it work? Is this not something that can be done with javascript from a popup window?
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Sorry if I was not clear. It shouldn't work because the name does not reference the original window in HTML DOM from the pop up window, but the name of the pop up window from the document would reference it's window element. Now if you want us to help you, you have to explain the document structure more thoroughly.
 

onyxmoron

New Member
Messages
37
Reaction score
0
Points
0
Sorry, I don't know much about javascript as you probably guessed. I ended up doing what I needed to do in a different way and it works, but I'd still like to learn why what I did before didn't work. Please tell me if I'm totally confused - javascript is something I can't get the hang of no matter how many times I try or tutorials I read. :mad:

The document structure is like this:

Index.html is a document with two frames, frame_menu (navigation) and frame_body (content). Some of the pages that load in frame_body have links that create popup windows called popupwin, and some of the pages that load in popupwin have links that are supposed to target the _top of the original window.

I was going to use php to control what loaded in the frames, like index.html?menu=thing1&body=thing2, to get around the fact that you can't bookmark or link to specific content in a frameset. That's why the links in the popup window neeed to be able to target the url of the window with index.html in it. When a link had a script with a perumatation of window.opener.parent it worked correctly the first time a link was clicked since it was targeting the parent of the window that opened the popup. But then the frame that had spawned the popup wasn't there any more, so every time a link was clicked after the first time nothing happened. That's why I couldn't use window.opener.

So, I named the original window that started with index.html in it "originalwindow" and added window.parent.name="originalwindow"; to every page that loaded in frame_body to make sure the parent window was named "originalwindow" at all times. I know the name "originawindow" is being preserved because any normal link (like <a href="http://forums.x10hosting.com/programming-help/index.html?menu=thing1&body=thing2" target="originalwindow">) in any place (frame_body, frame_menu, or popupwin) always works correctly, but I couldn't find out how to write a script that targets originalwindow by name and changes its url. Something like originalwindow.location.href = 'http://www.google.com'; was the only thing I could think of but of course it didn't work.

I know this is overly complicated and I'm probably doing things totally wrong but I'm trying to use it as an opportunity to learn by figuring it out. I know "frames are bad" and "popups are bad" but right now they're the only way to get the functionality I need. I've considered every alternative I can find and haven't found one that works as well for what I'm doing as frames. But I'm trying to write everything in a way that can easily be changed later if I do find a way that's better, hence all the php and javascript. (This site will have a very large number of pages and I'm concerned about having to re-write all of them if it changes to non-frames later.)

After working on it for a while I'm thinking of going to go back to doing it normally instead of using PHP. It's too slow and jarring since the whole frameset reloads every time you click a link, plus you lose your place in the menu whenever you click a link. The whole reason I'm using frames is so the user's place in one frame can be preserved while the other frame changes. (I tried using anchors in the menu pages so when the menu reloaded in the frame it would have the thing you clicked at the top, but it still felt very awkward.)

Sorry this is so long, I hope it makes sense :nuts:
 
Last edited:
Top