Forum Moderators: open

Message Too Old, No Replies

Pop-under when user leaves site

looking for a script

         

Adam_C

6:56 pm on Aug 6, 2004 (gmt 0)

10+ Year Member



All the ones I've tried so far use onunload="function()" in the body tag, and haven't distinguished between leaving a page and leaving a site.

Can anyone point me in the direction of a script that will:

- launch a pop-under when a user leaves my site
- does not launch the pop-under when they simply leave a page with the script on to go to another page on my site

and ideally

- doesn't lauch a pop-under when a user clicks a link to an external site that opens in a new window
- doesn't require anything to be added to the <body> tag

RonPK

10:35 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It can be done, I think, by attaching some event handlers to the body and to all hyperlinks on the page, but have you considered
* that many users don't like pop-unders
* that they are blocked by many browsers, including the soon to be released update to IE6/WinXP?

Adam_C

11:02 pm on Aug 6, 2004 (gmt 0)

10+ Year Member



yes, considered it all

still want the script!

Rambo Tribble

2:53 am on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Attaching an event handler to links would not, unfortunately, be likely to handle the case of a user leaving your page by selecting one of their bookmarks or by typeing in the URL. I think sticking with onunload is your best bet. I'd say set a global variable as a flag, using the .js file; an internal link (using onclick) could set the flag to, say, "1" to prevent the popup. Something like:

var link_glob=0;
window.onunload=function(){
if(link_glob==0)popunder_code;
}

Your links would look something like:

<a href="http://mysite_files" onclick="link_glob=1;">My Page</a>

dvduval

3:37 am on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think of an exit popup as reinforcement to never visit the site again. From an marketing point of view, I think exit popups are a mistake.

digitalv

6:36 am on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One time I made a purchase from a site and when I closed my browser a popup appeared with a coupon for 25% off my next purchase. Not all popups are bad, including ones that appear when the user leaves the site. The poster was just asking for some code, not a lecture in your particular flavor of popup ethics.

Anyway ... isn't there a way to accomplish this with cookies?

Adam_C

10:25 am on Aug 7, 2004 (gmt 0)

10+ Year Member



Cheers digitalv, you're right - I just want the script, not a lecture.

I would rather not have to stick anything extra in all my internal links. Could they by default not be set to not launch the pop-under?

I don't mind sticking with onunload, btw.

RonPK

12:41 pm on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Event handlers can be attached to links in a way similar to how Rambo T attached the onunload event handler to the window object. The document object has a property called links, which is an array that contains references to all the hyperlinks in the document. Loop through that collection and attach the onclick event handler to each link:

var alllinks = document.links; 
for (var i = 0; i < alllinks.length; i++) {
alllinks[i].onclick = function() {link_glob=1;};
}

This script should be executed when the document is fully loaded, i.e. by putting it at the end of your page or by triggering it with document.body.onload or <body onload="...">

Disclaimer: untested.

RonPK

12:46 pm on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW, this is a discussion board, not a write my script - service, so comments and opinions can be expected whenever someone asks starts a thread about a controversial technique.

I don't see how it could be done with cookies; can't think of a step-by-step algo including cookies, as they don't execute script. Correct me if I'm wrong.

Rambo Tribble

1:24 pm on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suppose you could make your script check the href value of each link before executing, using regular expressions to identify ones that were associated with your domain, then set the flag for those that are.

Rambo Tribble

3:23 am on Aug 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should give you what you need:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var link_stat=false;
window.onunload=function(){
if(!link_stat)alert("whoa"); //here, instead of the alert you would have your popunder code
}
function testLinks(){
var ln=document.links.length;
for(var i=0;i<ln;i++){
document.links[i].onclick=function(){
var test_url=/javascript:alert\('d/i;
// test_url is a regular expression to match "javascript:alert('d" ~etc. domain
//set the expression, between the forward slashes, to match your domain
var lnk_url=this.href;
link_stat=test_url.test(lnk_url);
alert(link_stat);
return true;
};
}
}
</script>
</head>
<body onload="testLinks();">
<p>
<a href="javascript:alert('duh');history.go(0);">
<!-- the history.go(0) method is invoked to simulate the setting of link_stat at page load -->
Click here for a "javascript:alert('duh');" URL test</a>
</p>
<p>
<a href="http://www.webmasterworld.com/forum91/2193.htm">Click here for this thread's URL</a>
</p>
<p>
<a href="javascript:alert('d\'oh');history.go(0);">Click here for a "javascript:alert('d\'oh');"" URL</a>
</body>
</html>

LeglessTable

3:45 pm on Sep 2, 2004 (gmt 0)



This is good code, but it doesn't factor in someone closing the window, refreshing, or using the next and back buttons.

Bernard Marx

5:33 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...or turning off their computer, and jumping out of the window.

Rambo Tribble

4:36 am on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Onunload is actuation agnostic. Also, since the JavaScript execution context is the window, when the window changes so does the execution context. In other words, onunload happens.

I've always been led to believe that the jumping out windows is more of a C++ thing; not so much JavaScript. Did I miss a memo?