[Javascript] Alert user when leaving site

Julien Nadeau junado at junado.com
Fri Sep 9 18:06:49 CDT 2005


Here's at least part of the answer. Using the "unload" event (set to  
body), you can get the current URL using JavaScript:

<html>
<head>
<meta content="text/html; charset=windows-1252" http-equiv=Content-Type>
<SCRIPT LANGUAGE="JavaScript">

function checkDomain() {
     var currentURL = location.href;

     protocolIndex = currentURL.indexOf("://");
     serverIndex = currentURL.indexOf("/", protocolIndex + 4);
     urlResult = currentURL.substring(protocolIndex+3, serverIndex);

     alert(urlResult);
}

</script>
</head>
<body onunload="checkDomain();">

<a href="http://www.yahoo.com/">Leave</a>

</body>
</html>

Clicking the link will popup an alert with the domain in it. What has  
to be done is try to compare this string to the destination domain. I  
have no idea how to do this automatically, but you could modify my  
function to add it to every link in the page and add an destURL  
argument to compare (this could be done automatically by scanning the  
DOM for every <a> element and adding events to each of theses).

Julien Nadeau
junado at junado.com

Le 05-09-09 à 18:42, Mike Dougherty a écrit :

> I'd like to be able to alert a user they're about to leave a page  
> full of unsubmitted form data... if there is a script that does  
> what Judah is asking for, i could probably make it work for me too.
>
> thanks.
>
> On Fri, 09 Sep 2005 15:49:05 -0500
>  Judah Frangipane <judah-fc at t8design.com> wrote:
>
>> Does anyone have a script to alert a user when they leave a  
>> domain? I know they are annoying but it's for a bank site.
>> Best Regards,
>> Judah Frangipane
>> _______________________________________________
>> Javascript mailing list
>> Javascript at LaTech.edu
>> https://lists.LaTech.edu/mailman/listinfo/javascript
>> __________________________________________________________
>> This message was scanned by ATX
>> 5:50:43 PM ET - 9/9/2005
>>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20050909/a7ed98cd/attachment.htm>


More information about the Javascript mailing list