Forum Moderators: open

Message Too Old, No Replies

global script that runs when link clicked

global script that runs when link clicked

         

net2004

2:10 am on Mar 26, 2004 (gmt 0)

10+ Year Member


I want to make a popup that warns a visitor they are leaving a site only for external links. Problem is I don't want to have to put an OnClick Event for 100+ links in my site. Is there a way to do the following in a more global fashion?

1) the link is clicked (most of these are target="_blank" and the onUnload does NOT run as far as I know.)

2) a script runs somehow that checks the URL and domain name and if the link is an external domain name it runs the pop up function.

3) the user can then click CONTINUE or CANCEL.

4) IF they click CONTINUE Then the URL that was determined from the previous funtion somehow loads into a new window. If they click Cancel the window just closes and the old site is still in the background.

i have seen sites that use onClick on every link that leaves the site but there must be a way to do this dynamically where I don't have to put this command on every link I hope!?

(ASP creates this popup dynamically and the URL is passed as an attribute on the end of the link to the ASP page. )

<table cellspacing="0" width="150">
<tr>
<td><a href="http://www.external_link_URL.com" onclick=" window.close();" target="_blank"><img src="images/btn_continue.gif" width="67" height="15" alt="" border="0"></a></td>
<td><a href="javascript:window.close();"><img src="images/btn_cancel.gif" width="67" height="15" alt="" border="0"></a></td>
</tr>
</table>

Thanks!

VC

Rambo Tribble

3:26 am on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid that the standard link function, that of initiating the download of the page at the address in the anchor tag's href attribute, is not a JavaScript event, but an HTML action that is outside of JavaScript's direct control or even monitoring.

It is hard to imagine how JavaScript could trap the click event without an onclick or onmousedown event handler in the anchor tag.

As links are executed locally, I don't think there is anything server side that would be of use, either.

net2004

3:50 am on Mar 26, 2004 (gmt 0)

10+ Year Member



From what I have seen so far this seems to be the case unfortunately. : ( I'm still open to any magical solution though. : )

Thanks.

ajkimoto

3:22 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



net2004,

How about something like this:

<script type="text/javascript">
<!--

function click(e){
//if IE
if(typeof event!='undefined'){

//if the object clicked on has an href
if(event.srcElement.href){
alert(event.srcElement.href)
}

//else assume w3 standard (ns6+, mozilla)
}else{

//if the object clicked on has an href
if(e.target.href){
alert(e.target.href)
}
}
}

//global click handling
document.onclick=click

//-->
</script>

Just add some code to determine whether or not the link is local and you should be set. BTW, this code does not work in old browsers like ns4, but there is an event model for that as well--just not included here.

Hope this helps,

ajkimoto

Rambo Tribble

4:24 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice work, ajkimoto. Gettin' jiggy wit' da DOM.