Forum Moderators: open

Message Too Old, No Replies

Focusing an already opened window

         

leeshanokb

4:13 pm on Dec 11, 2003 (gmt 0)

10+ Year Member



Hey everyone,

I'm trying to use Javascript to dynamically create a hyperlink that will open a new window, with the stipulation that if the window is already opened, it is merely brought into focus instead of having a second identical window open.

I've written some code to do just that, and I'm getting mixed results. Basically the generated hyperlink will open a new window, but for the life of me I can't get it to catch and bring the new window into focus.

It appears as though the code isn't catching the else statement.. but I'm not sure where things are breaking down (I'm doing this in Notepad, and then loading it into IE). On that note, does anyone know of any good Javascript IDEs? :o)

I've pasted the code below:

document.write('<a href="#" onClick="')
document.write("if ")
document.write("(!")
document.write(windowname)
document.write(" ¦¦ ")
document.write(windowname)
document.write(".closed) {")
document.write("var ")
document.write(windowname)
document.write(" = ")
document.write("window.open('")
document.write(link)
document.write("','")
document.write(windowname)
document.write("','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');")
document.write("} else {")
document.write(windowname)
document.write(".focus();")
document.write("}")
document.write('">')
document.write(title)
document.write("</a>")

TrinkDawg

10:40 pm on Dec 11, 2003 (gmt 0)

10+ Year Member



Hi, can you try something like this:

<script language="JavaScript">
<!--
var childWindow = null;
var yourLink = 'http://www.webmasterworld.com';

function doIt()
{
if (!childWindow)
{
childWindow = window.open(yourLink, 'childWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
}
else
{
try
{
childWindow.focus();
}
catch (e)
{
childWindow = window.open(yourLink, 'childWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
}
}
}
-->
</script>

<a href="#" onclick="doIt();">Your title here</a>

This worked for me in IE6. I'll leave the transformation to document.writes to you.

-Mike