Click to See Complete Forum and Search --> : JS Window


traven
01-08-2007, 08:05 AM
I have a JS function in my web page that receives a url variable.

This web page is a pop up window with some news. When i click in one single news, it calls the js function.

I want to detect if there's no window opened, then open a new window with the url in the parameter of the function. Other than that, if there's any browser opened window, then it reloads that window with the new url.

how can i do that?

function abrirMateriaPop(elementoId,url)
{
if(!window.opener || window.opener.closed)
{
window.open('http://www.globo.com');
}
else
{
window.opener.location.href='http://www.globo.com';
}

}

It works in IE but doesn´t in firefox 2.0
Also, If the user open a new IE window (in case of IE test), this is not counted as an active window.

konithomimo
01-08-2007, 09:15 AM
Well, your script is searching for the opener of the window you are in. Usually you will check to see if the desired window existed, in this case we know that it did, otherwise the window you are in would not be opened . . . most likely, but even so, your code is the correct way to check for a window. If it was me, I would change one thing though. Set a variable, lets call it 'newwin' to be null, outside of the function, and then inside the function assign it a value.

var newwin = window.opener;
function abrirMateriaPop()
{
if(!newwin|| newwin.closed)
newwin = window.open('http://www.globo.com');

else
newwin.location.href='http://www.globo.com';
}

konithomimo
01-08-2007, 09:18 AM
In this case it was easier to set the variable to the window.opener, since that will return null if the opener was never there, or return the object we want if it did exist, or is still there.

traven
01-08-2007, 11:51 AM
Ok, It works. But there is a problem in firefox 2.0 when there is only the pop up window. I click in the news, then it opens in a new tab in the pop-up window, not in the new window that should be.

CODE:

function abrirMateriaPop(elementoId,url)
{
if(!window.opener || window.opener.closed)
{
window.open('http://www.globo.com');
}
else
{
window.opener.location.href='http://www.globo.com';
}

}

konithomimo
01-08-2007, 02:47 PM
try self.opener instead of window.opener. Some other browsers use opener.opener to reference the parent window, but for most self.opener will do just fine.