PDA

View Full Version : onUnload site exit message (exclude current domain)



snakecrosscrown
09-14-2005, 02:33 AM
I am trying to launch a popup window when someone exits my site (as in when they press 'home' in their browser). I do not want the popup to launch when they click on an internal link.

I was able to get a simple script to work using 'onUnload'. The problem is that it opens a popup when the navigation menu is clicked (a link to another page on my site).

I wanted to exclude my domain from the onUnload function. Is this possible?

I think I want to combine this

<script language="javascript">

/*
Auto center window script- Eric King (http://redrival.com/eak/index.shtml)
Permission granted to Dynamic Drive to feature script in archive
For full source, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
*/

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}

</script>

and this

<BODY onUnload="NewWindow('http://yahoo.com','name','400','400','yes');return false">
with this

<script language="JavaScript1.2">
<!--

//Open offsite links in new window option- By Jessica Hammer
//Heavily modified by Dynamic Drive
//Visit http://www.dynamicdrive.com for this script

//1)Enter domains to be EXCLUDED from opening in new window:
var excludedomains=["dynamicdrive.com", "javascriptkit.com"]

//2)Automatically open offsite links in new window? (1=yes, 0 will render a checkbox for manual selection)
var auto=1

var excludedomains=excludedomains.join("|")
rexcludedomains=new RegExp(excludedomains, "i")

if (!auto)
document.write('<form name="targetmain"><input type="checkbox" name="targetnew" checked onClick="dynamiclink()">Open off-site links in new window</form>')

function dynamiclink(){

if (auto||(!auto&&document.targetmain.targetnew.checked)){
for (i=0; i<=(document.links.length-1); i++) {
if (document.links[i].hostname.search(rexcludedomains)==-1&&document.links[i].href.indexOf("http:")!=-1)
document.links[i].target="_blank"
}
}
else
for (i=0; i<=(document.links.length-1); i++) {
if (document.links[i].hostname.indexOf(mydomain)==-1)
document.links[i].target=""
}
}

if (auto)
window.onload=dynamiclink

// -->
</script>
Does anyone know how to combine these? If you know of a better way to launch a site exit message, please tell me (btw, this is not an ad, it is a epilogue-type message like "Thanks for visiting" or similar)

snakecrosscrown
09-14-2005, 03:34 AM
Actually, what I'm looking for is more like JavaScript Kit's FairWell window launcher at http://www.javascriptkit.com/script/cut65.shtml but I still would like it to ignore pages with my own domain in the url. Any ideas?

Twey
09-14-2005, 10:51 AM
We had something like this recently.


<script type="text/javascript">
var leaving = true;
function checkUrl(href) {
leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));
// add in more things to include in the line above as you see fit
}
function pop() {
if(leaving) window.open("thanks.htm");
}
window.onunload = pop;
var e = documents.getElementsByTagName("a");
for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";
</script>
<a href="http://www.example.com/">Click here and you'll see a popup window</a>
<a href="internal.htm">Click here and you won't</a>
It's rather early in the morning, so there are probably a couple of bugs :p

snakecrosscrown
09-15-2005, 12:26 AM
I don't have external links on my page. I'm trying to get a popup window to appear when someone presses back or home or types in a new url in their browser. I can get onUnload to do this but I want it to exclude internal links on my site (page to page navigation).

Twey
09-15-2005, 08:52 AM
The above will do this.

snakecrosscrown
09-15-2005, 08:02 PM
// add in more things to include in the line above as you see fit :confused:

Maybe I forgot to mention I'm a complete newb :o . I'm really sorry but what do I need to edit? Thanks again for your help.

Twey
09-16-2005, 10:42 AM
leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));
Add in || (href.indexOf("somethingElseThatDenotesAnExternalURI") > -1). The above will probably be OK, as long as all your external links use http(s):// and don't leave the browser to fill it in.

chicagoseo
02-09-2006, 05:27 AM
Hi Twey, I am in the exact same boat as snakecrosscrown. I am a dumb marketing guy who can barely read HTML but have been forced into due diligence on this one.

So we have a web site with hundreds of pages, will your code still work or do we have to name all of the internal pages that we dont want the pop up to trigger on?

Thanks,

Andrew

Twey
02-09-2006, 04:10 PM
Yes, it will all work, so long as the pages are on the same domain. Instead of:
leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));you might want to use
leaving = (((href.indexOf("://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));This should allow you to use absolute URIs for your pages.

chicagoseo
02-12-2006, 11:13 PM
Thanks Twey! All of our internal links are indeed on the same domain. I can't emphasize enough how dumb I am when it comes to coding, expecially JS :) So we are popping a usibility survey, let's say the popup url is http://www.survey.com and our site is http://www.widgets.com

Can I just simply paste this chunk of code in between body tags?

What exactly do I find & replace with the url examples (pop up url: www.survey.com, my site = www.widgets.com) ? (This is my biggest challenge as I literally don't know what characters are reserved words and which I am supposed to replace/edit)


<script type="text/javascript">
var leaving = true;
function checkUrl(href) {
leaving = (((href.indexOf("://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));
// add in more things to include in the line above as you see fit
}
function pop() {
if(leaving) window.open("thanks.htm");
}
window.onunload = pop;
var e = documents.getElementsByTagName("a");
for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";
</script>
<a href="http://www.example.com/">Click here and you'll see a popup window</a>
<a href="internal.htm">Click here and you won't</a>


Also, are the below lines of code optional? I am not sure I understand if they will visibly appear as clickable links on our site and I can leave them out?

<a href="http://www.example.com/">Click here and you'll see a popup window</a>
<a href="internal.htm">Click here and you won't</a>


Any further insite would be greatly appreciated!

Andrew

Twey
02-13-2006, 12:24 AM
Also, are the below lines of code optional? I am not sure I understand if they will visibly appear as clickable links on our site and I can leave them out?Yes, those are just examples.

Can I just simply paste this chunk of code in between body tags?For best results, put it above your </body> tag.

What exactly do I find & replace with the url examples (pop up url: www.survey.com, my site = www.widgets.com) ? (This is my biggest challenge as I literally don't know what characters are reserved words and which I am supposed to replace/edit)

if(leaving) window.open("thanks.htm");thanks.htm can be any URI, local or remote. Any character allowed in an URI is allowed in a Javascript string, you needn't worry about that.

rdctaylor
07-26-2006, 06:35 PM
When I use the code from this post I get the following runtime error:

Error:'documents' is undefined

Below is my exact code. I get this error with IE 6 and Firefox.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
var leaving = true;
function checkUrl(href) {
leaving = (((href.indexOf("://") > -1) || (href.indexOf("www.") > -1)) && (href.indexOf(window.location.host) == -1));
// add in more things to include in the line above as you see fit
}
function pop() {
if(leaving) window.open("thanks.htm");
}
window.onunload = pop;
var e = documents.getElementsByTagName("a");
for(var i=0;i<e.length;i++) e[i].onclick = "checkUrl(this.href)";
</script>
<a href="http://www.example.com/">Click here and you'll see a popup window</a>
<a href="internal.htm">Click here and you won't</a>
</body>
</html>