PDA

View Full Version : Popup on page exit to external source



lhr
05-23-2010, 10:43 PM
To become more familiar with Javascript, I've been trying to put a nice exit popup on my website, I ended up choosing this one from DD mainly because it looked so good right out of the box: http://www.dynamicdrive.com/dynamicindex17/interstitial.htm (Interstitial Content Box (v1.1))

However, my attempts to edit the script's interstitial.js file so that it only loads on exit of the page have, well, not got me quite to that point :o

I've been reading (via tutorials, looking at other scripts, searching previous answers on SS) that I should be putting this sort of thing in an unLoad event on the body tag of my website. But I have 3 specific questions that I would love to get your feedback on so that I might get this working.

1) I believe the line "page_request.onreadystatechange=function()" is where it actually calls the thing to load, is that right? So I think this is the sort of area where I should be changing it. At least the handful of lines above it, I'm sure that's where it's deciding if to load.

2) Some tutorials for exitpop scripts mention that it's old hat to be using the unload thing on a body tag, but I can't find one that suggests to use a better alternative (I did find one site but it required registration to read their forum, and it rejected my email to register there :rolleyes:) - what is considered a better way to check "user is going to an external site"?

3) I do wonder what happens if someone has cookies disabled, like on my local library's computer, which spits out an error when cookies are needed by websites. I can test this later, but was just wondering.

:) I guess question1 is really the important one :p:o

lhr
05-23-2010, 10:48 PM
Oh dear, I have just seen the more appropriate forum (http://www.dynamicdrive.com/forums/forumdisplay.php?f=2) to post this question in, but I can't seem to move this post when I edit it, nor delete to start over. Sincere apologies to the moderators, my first post and it's in the wrong place.. :/

djr33
05-23-2010, 11:53 PM
Your post has been moved. Welcome to the forums.

lhr
05-24-2010, 09:07 AM
Great, thanks for that djr33 :D

djr33
05-24-2010, 05:29 PM
"user is going to an external site"You can tell when someone is leaving by <body onUnload=".....">

I'm not sure about how you could tell if they are leaving for an external site. You could obviously attach an event to every link that checks if this.href contains your site (or is relative) or not.
But this won't account for them closing the window (since there's no URL there).

One way to attempt this would be to add to all of your local links a property so that it knows not to give the popup; however, it would then not work for hitting the back button and other things.

I really can't think of a good way to do this. Maybe there is more information available somehow with just using the onUnload event, but I wouldn't know where to start with that. Try google.


Another way around all of this which would be annoying to the user but possible is to create a "popunder" which is like a popup but it is placed underneath the current window. Then it remains until the window is closed. You could even have it listening to see if the parent window is ever closed, then if so it can pop up. That's as close as I can think of to something that would work like this.


And I'd say be careful: any sites that force me to do things other than close then when I want to leave annoy me: I probably wouldn't return. Odds are that your site really isn't that special and if users care they'll stay on their own. For whatever reason your site may be an exception (or you may think it is), so go ahead if you want, but just think twice about interfering with the normal user experience... more often than not it's bad.


Regarding cookies:
There are 3 ways mentioned: "permanent" cookies, session (cookies), and based on a probability.

If you are using cookies to see if someone has already received the popup, then you will ALWAYS serve the popup until a cookie is stored: on a system where cookies cannot be stored you will always get the popup.
For the probability option that will work fine on any system (at least it should...).

I'm not sure exactly how this script functions (I haven't looked through all of it, and it's a bit beyond my knowledge in Javascript), but you could have a backup: if the script fails to load a cookie then it should revert back to using the probability. Of course that's kinda messy, but it's better than having it on every page load. Or, of course, you could just disable it if cookies don't work.
(So do it in this order: 1) check if the cookie exists; if not: 2) store the cookie; 2b) if that fails, assume they've already seen it [or whatever alternative you'd like]; 3) display the popup; [If the cookie did exist, do nothing more].)

And if the computer issues a warning when a cookie is saved, I don't think the script can do anything about that at all.

The only way around the "no cookies" problem would be to use something else to store the information, and Javascript doesn't have any other options.
Two to consider would be flash (hard to use to store info, but it is possible) which is probably not worth it; and if you want to use server programming (like PHP, ASP, CGI, etc) you can use that to track page loads. It won't be related to the Javascript per se, but it will allow you to dynamically generate Javascript commands: basically once per session (this is something you can do only serverside, not requiring cookies [even though usually it's based on cookies]) you can output the JS command to show the popup. Then don't output that again (using PHP, etc) during that session. This way PHP will decide when to show it and not Javascript. However, this is still messy and will require a lot of extra levels. It'll work (probably) without cookies, though.



By the way, if this is crucial to your site, you could try using frames (or iframes, whatever you'd like). This way you can have the popup happen every time the main page loads (but it'll only load once) and won't have to worry about cookies. It will also be very simple to find out when they are going to an external site: any time they leave the [main] page. There are still problems, of course, such as what happens when a user gets annoyed with the frames and decides to visit one of the pages separately. And then you've got all the general problems of frames. It's possible, though.

lhr
05-25-2010, 07:52 AM
Yikes, having read your reply (twice, just to make sure I'm understanding what I can!), I can see that I've really bitten off more than I can chew here. I'd imagined a few tweaks of code here and there, maybe a few external references that could explain, using another example, why those tweaks should/have been made... and I hadn't stopped to consider the IMPLICATIONS of this thing. I guess, if it's an obvious/useful thing to do, it would already exist, right :D

For now, I've bookmarked this thread and will head back to some of your suggestions after getting more involved in Javascript - it's a tad over my head right now, albeit I do understand the general theories of implementation that you outlined - thank you for giving so much detail:D Glad I decided to register now, I've been lurking (whilst learning) for a while :o

traq
05-25-2010, 02:27 PM
I got this here long ago (I'm not sure I remember who wrote it), but it seems to work in most cases. I'd reiterate djr's caution, however: when someone wants to leave, they want to leave. In that sense, no exit popup is a good one.

<script type="text/javascript">
var domain='http://www.domain.com'; // Set your domain here.
window.onbeforeunload = function()
{
if(this.location.href.indexOf(domain)==-1)
{
return "Are you sure you want to exit?";
}
}
</script>

djr33
05-25-2010, 05:11 PM
Interest, Traq. That answers the question generally, though it still leaves a few options: the user closes the window or types in a url (does this work??), or the subdomain changes on the same site.

But in general that looks like a good solution, ignoring the questions of whether it should be done or not.

traq
05-25-2010, 07:17 PM
just closing the window: I don't think it would work. Haven't tried, though.

typing in a url (as opposed to clicking a link) does trigger the popup.

subdomains: I think going to a subdomain would trigger the popup, as subdomains are not technically "the same site." var domain, I'm sure, could be changed to an array with all of the (sub)domains you would want counted as "the same site," but I'm not good enough with js.