Javascript Close Browser or goto external site function

Posted by aaroninfidel on Stack Overflow See other posts from Stack Overflow or by aaroninfidel
Published on 2010-04-06T06:42:34Z Indexed on 2010/04/06 6:43 UTC
Read the original article Hit count: 309

Hello, I've been trying to figure out how I can go about creating a javascript function that will only be called when a user is accessing an external site or closing the window to the current site they're on.

So far I've come up with this, any help is greatly appreciated.

<script type="text/javascript">
window.onbeforeunload = confirmExit;
function getAd()
{
    var myAds = new Array();
    myAds[0] = "http://localhost/link1";
    myAds[1] = "http://localhost/link2";
    var randomAd = Math.floor(Math.random() * myAds.length);
    return myAds[randomAd];
}
function confirmExit(){
    alert(getAd());
    return false;
}
</script>

© Stack Overflow or respective owner

Related posts about javascript-events

Related posts about JavaScript