Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window Openning detection

Status
Not open for further replies.

abakar

Technical User
Mar 24, 2003
2
DE
I was wondering if there was a way to detect if the current browser
openned a new window. For example, I have a link like <a
href=&quot; target=&quot;_blank&quot;>text</a>

And I would like to know if a window was openned using this like (the
_blank target opens a new window, but I do not think it has a name) I
would like to be able to detect this without editing the <a> tag with
something like onClick, but just be able to detect if the window is open
(or was openned) using a if statement without having to alter the HTML
code itself.

Thak you
 
Name the new window, then use:

windowName.closed

This returns false if the window is open, so you may want to invert it, sometimes seting things false gives problems.

if(!windowName.closed){do whatever cause the window is open}

Also, I guess the windowName returns true if it exists &quot;Alright whatever man, I'll hook up the hair, but I aint touchin the ring...Cause I'm still a pla--yer&quot;
 
how would you name it using the code above? can you name a window that you open using target=_blank? adam@aauser.com
 
I think that you can't escape from using the onClick event. Here is how you can do it without editing the <a> tag.

<script language=&quot;JavaScript&quot;>
function checkLinks(){
for (var i=0; i<document.links.length; i++)
if (document.links.target==&quot;_blank&quot;)
document.links.onclick = newWinAlert;
}

function newWinAlert(event){
// do what you like;

}

document.onload=checkLinks;
</script>

WARNING: I didn't test it but it should work with both IE and NN.

Good luck! Ivan Pavlov (ip@thehyper.net)

Brainbench MVP for JavaScript
 
I tried to use the following code with a onLoad=&quot;checkLinks()&quot; in the body tag, but it seems the window.document.links.onClick = newWinAlert; executes even if the click on the link is not done (it executes on page load...) anything else I could do...or did I do anything wrong?


<script language=&quot;JavaScript&quot;>

function checkLinks(){

for (var i = 0; i < document.links.length; i++)
{
if (document.links.target == &quot;_blank&quot;)
{
window.document.links.onClick = newWinAlert;
}
}
}

function newWinAlert()
{
alert(&quot;It worked&quot;);
}

</script>
 
I think you want to open the window properly, rather than using the _blank atrget idea.

<a href=&quot;javascript:eek:pen('page.html','windowName','attributes')&quot;>Link</a>


This gives you absolute control over the new window.

luciddream -As far as I can see, there is no way to reference a window created by &quot;target=_blank&quot;, unless there is some child array somewhere, but I can't find it right now!

- Check out the FAQ which gives all the attributes, for a new window (width, height etc.) &quot;Alright whatever man, I'll hook up the hair, but I aint touchin the ring...Cause I'm still a pla--yer&quot;
 
Abakar,
You missed the index when reffering to the links collection, so your code points to the collection and not to a member of it.

You also should set a return value for the newWinAlert() - if it returns &quot;true&quot; the new window is opened, else it is canceled. So I replaced your function &quot;alert(...)&quot; with &quot;confirm(...)&quot; just to demonstrate this.

bellow is the corrected code, I tested it in IE 5.5 and NN 4.7 and it worked:

<script language=&quot;JavaScript&quot;>

function checkLinks(){

for (var i = 0; i < document.links.length; i++)
{
if (document.links.target == &quot;_blank&quot;)
{
window.document.links.onclick = newWinAlert;
}
}
}

function newWinAlert()
{
return confirm (&quot;Do you want a new window?&quot;);
}

</script>

Ivan Pavlov (ip@thehyper.net)

Brainbench MVP for JavaScript
 
sorry Abakar, I had to turn-off the &quot;Process TGML&quot; option of the message or it takes for itallic formating. I just noticed that you probably had the same problem.

Here is the code one more time (may be the only real problem that you had is the char case in &quot;onClick&quot; correct is &quot;onclick&quot; and it is case sensitive ). This time I include the whole code that I used for testing:


<head>

<script language=&quot;JavaScript&quot;>
function checkLinks(){

for (var i = 0; i < document.links.length; i++)
{
if (document.links.target == &quot;_blank&quot;)
{

window.document.links.onclick = newWinAlert;
}
}
}

function newWinAlert()
{
return confirm (&quot;Do you want a new window?&quot;);
}
</script>
</head>

<body onload=&quot;checkLinks();&quot;>
<a href=&quot;&quot; target=&quot;_blank&quot;>This is the test link;</a>
</body>
</html>

Ivan Pavlov (ip@thehyper.net)

Brainbench MVP for JavaScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top