Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Warning that visitor is leaving site

Status
Not open for further replies.

robert89

Technical User
Nov 19, 2003
125
CA
Hi there,
Has anyone seen a cookie script that will warn a visitor that they are leaving a specific site. i.e. when they click on a link that is external to mysite.com. We want a cookie that (uses an alert?) to warn the visitor that they are about to leave our site and go to another site.

Any help sure appreciated.

Bob
 
You can easily do this by walking through a collection of all the <a href> nodes in the current page... and attaching an onclick to them if their url is considered "external".

Look at using document.getElementsByTagName('a') to get a collection of all the <a href> nodes. You can then use length() to find out how many there are - so you can set up a loop. Then you can examine the href src for each - maybe assuming that if they start with http:// then they are external... and attaching an onlick to them.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
We want a cookie that (uses an alert?) to warn the visitor that they are about to leave our site and go to another site.

No can do. Cookies cannot warn people - they are data storage. You'll have to use another method that has nothing to do with cookies, much like the one Jeff has suggested above.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks BabyJeff, but you've gone way over my head. Dan went to Code Couch really like some of your efforts.

Bob
 
Hey Bob...

Here is a sample page that shows what I was talking about. If the user has javascript it will work... otherwise it won't interfere with anything.
Code:
<html>
<head><title>Example</title>
<script type="text/javascript">
function alertUser() {
	if (this.href.indexOf('http')==0 || this.href.indexOf('[URL unfurl="true"]www')==0)[/URL] {
		alert('You are leaving our site now - have a safe journey!');
	}
}

function initPage() {
	var hrefs = document.getElementsByTagName('a');
	for (var i=0,max=hrefs.length; i<max; i++)
		hrefs[i].onclick = alertUser;
}

window.onload = function () { initPage(); };
</script>
</head>

<body>
<p>There are some links on this page...</p>
<ul>
	<li><a href="/contact.html">Contact</a></li>
	<li><a href="/intro.html">Introduction</a></li>
	<li><a href="/products.html">Products</a></li>
	<li><a href="/services.html">Services</a></li>
	<li><a href="[URL unfurl="true"]http://www.google.com">Google</a></li>[/URL]
	<li><a href="[URL unfurl="true"]http://www.google.co.uk">Google[/URL] UK</a></li>
</ul>
</body>
</html>

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Hi Jeff,
Thanks very much. I can take a look at this and learn from it. Really appreciate your help.

Bob
 
No worries. More and more you will see that kind of code... something that adds functionality to existing HTML - but only loading that functionality after the page has loaded (and when javascript is enabled). This is one of the fundamentals driving the whole "Web 2.0" buzz - seperate out the markup, the presentation (css) and extra "enhanced" functionality so that the code remains usable across many different applications and uses.

Let us know how you go!
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top