PDA

View Full Version : How do I get this popunder script to load only upon Exit?


Fred
11-04-2002, 10:51 PM
This is a great popunder code, but how could I get it to load the popunder window, only when somebody leaves or closes my page? Right now it loads immeadiately with the page. I want to reconfigure it to load the popunder, the instant a visitor closes or leaves the page. I have it set at: http://www.cyberserious.com

Also, what exclusion code will work in individual link tags to prevent this script from executing when the visitor clicks certain links on my page?


<script>

//Pop-under window- By JavaScript Kit
//Credit notice must stay intact for use
//Visit http://javascriptkit.com for this script

//specify page to pop-under
var popunder="http://www.cyberserious.com/subscribe.html"

//specify popunder window features
//set 1 to enable a particular feature, 0 to disable
var winfeatures="width=400,height=450,scrollbars=1,resizable=0,toolbar=0,location=0,menubar=0,status=0,directories=0"

//Pop-under only once per browser session? (0=no, 1=yes)
//Specifying 0 will cause popunder to load every time page is loaded
var once_per_session=0

///No editing beyond here required/////

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function loadornot(){
if (get_cookie('popunder')==''){
loadpopunder()
document.cookie="popunder=yes"
}
}

function loadpopunder(){
win2=window.open(popunder,"",winfeatures)
win2.blur()
window.focus()
}

if (once_per_session==0)
loadpopunder()
else
loadornot()

</script>



-----------------------------------------------------

http://www.internet-marketing-ideas.com

krycek
11-04-2002, 11:13 PM
try calling the function from the onunload() event :)

::] krycek [::

EDIT: link to MSDN: http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events.asp

Fred
11-04-2002, 11:39 PM
Krycek,
Where in the code would I want to insert unload()?

Would I substitute one of the existing functions with unload()?

Will I need to add anything inside the <BODY> tag?

Also, what piece of code could I add to some of my individual <a href= tags to cause the script to not load the popup when the visitor clicks those particular links? Ie: another page within my site.
Thanks,
Fred

krycek
11-04-2002, 11:54 PM
OK...

the onunload() call should be put in the body tag:

<body onunload="loadcheck()">

you should also make the following adjustments:

function loadcheck() {
if (once_per_session==0)
loadpopunder()
else
loadornot()
}

...where the red lines are the ones to add :)

As for the last question... I am not entirely sure what you mean...

The script you are using stores a value in a cookie. The lines above check to see if the popup has already been displayed - that is what is stored in the cookie :) All I have done is to turn this into a function which you can then call when the page unloads, instead of whenever the page loads. :D

So, in answer, the popup will not load from any links, so long as it has been displayed once already. i.e. - the very first time someone visits a page, they will see the popup when they change page or close the browser. They will not see it again afterwards.

If I have misunderstood your question please rephrase it - but I did not totally understand it! :)

I hope that helps :)

::] krycek [::

Fred
11-05-2002, 02:16 AM
krycek,
Your suggested modifications to the code worked great, except the only problem is it now loads as a popup instead of a popunder behind the other browser windows.

Regarding my last question, what I meant is if I don't want the exit popunder to load when a visitor clicks a particular link, such as to another page in my site. I would only want the popunder to load when the visitor is actually leaving my site all together and not proceeding to my ordering page to order my products.
Fred

glenngv
11-05-2002, 02:24 AM
maybe these links could help:

http://codingforums.com/showthread.php?s=&threadid=6823
http://codingforums.com/showthread.php?s=&threadid=8563

krycek
11-05-2002, 12:30 PM
I think I get what you mean :)

By the way, I am not sure what you mean by the difference between a popup and a popunder, that is, I do not know why a new window would popup UNDER another...?

However, I did not grasp your aim entirely before. I now gather that you basically want the message to display ONLY when the user closes the browser... or leaves your site?

The trouble is, there is no simple way for you to acheive this, at least, not the second part. I do believe you can modify your code like this:

- remove the onunload() statement from the body tag
- add this line to the end of your script:

window.onunload = loadcheck()

That way, it *should* assign the onunload event to the window only, meaning page changes will not trigger it. Also, you can get rid of most of the code as well, because you would not need to use the cookies, as the script will only fire once per session because you can only close the window once! :)

If this is what you want, I can redo the script for you. However, as I get the impression that it is important to check if a visitor is leaving your site also, then you need to do something a bit more long-winded.

There are a number of ways to do this, and I am going to suggest two. The first one is to set a variable which all of your links use, for instance donotpopunder. Then, when a user clicks on one of YOUR links, the script would know not to do its thing. Other links would not set this variable, and so you would know a user is leaving your site.

Personally I do not like that approach - it feels messy, and although it would work, you would have to take time coding each of your links with your own extra bit of code.

The second way is a little more complicated, but I think will acheive a better result - if you can get it to work! ;)

I am sure you are familiar with the location object. Well, my idea is simple - either onunload or onbeforeunload check the value of the window.location.hostname (if you have your own domain, or else .href) and see what that is, and if it corresponds to your site.

Well, that is OK in principle, but I have just spent about half an hour playing with code and looking through properties and events at MSDN, and I cannot get it to return the NEW location, just the CURRENT one.

Similarly, there does not seem to be much in the way of associated event properties.

So, this is where I have to stop, because I do not know how to do what I have described. Perhaps someone else out there knows a method of acheiving this, and I would certainly be interested to know it! - but I haven't got a clue how to make it work :)

Good luck!

::] krycek [::

krycek
11-05-2002, 12:38 PM
UPDATE:

I have just had a quick look at the other threads linked to by glenngv (perhaps I should have done so first! ;)) and they provide a couple more ideas, but maybe still not what you are after.

If you JUST want to detect a window close, then there is a good script there - and, by the way, what I said about window.onunload in my previous post is not true, it triggers on page changes too :(

Otherwise, if you want to see if a link on your page takes a user outside your site, then using a hidden frame could work well :) simply have the code in the frame check the location of the visible frame onunload, and onload, and compare the two... if the location.hostname is not the same, then the site has changed :D

A combination of the two might work for you, but neither solve the problem of a user typing a new link into the browser themselves. That would get rid of a hidden frame, and you would have no way of checking.

I hope that helps a bit more :)

::] krycek [::