[thelist] onUnload event and popup

Roger Ly evolt at matchpenalty.com
Thu Aug 12 17:38:23 CDT 2004


>SO, my question is, is there a simpler method to check onUnload if the 
>user has either clicked an internal link or left the site?

We've done something similar here and while this may not be the
prettiest solution, it seems to work.

<script type="text/javascript">
var bShowPopup = true;
window.onunload = showPopup;

// This will make all onclick and links trigger our customized popup
// cancel function.  Any link on the page should be considered one
// that you don't want to a popup to appear for
function initializeClicks()
{
	if (document.all)
	{
		for (var i = 0; i < document.all.length; i++)
		{
			// if ((there is an onclick) || (this is a
link))
			// special case with onclick because there are
			// some onclick handlers on non-<a> tags.. blah
			// This basically takes any existing onclick
			// handler functions and appends the cancelPopup
// function to it.  cancelPopup does just that
			// it cancels the popup.
			if ((document.all[i].onclick != null) ||
(document.all[i].href != undefined))
			{
				document.all[i].oldClick =
(document.all[i].onclick) ? document.all[i].onclick : function() {};
				document.all[i].onclick = function() {
cancelPopup(); return this.oldClick(); };
			}
		}
	}
	// don't want the popup to appear when you submit a form
	// either.
	// replace any existing onsubmit handler with a new one that
	// adds a call to cancelPopup
	if (document.forms)
	{
		for (var i = 0; i < document.forms.length; i++)
		{
			document.forms[i].oldOnSubmit =
(document.forms[i].onsubmit) ? document.forms[i].onsubmit : function()
{};
			document.forms[i].onsubmit = function() {
cancelPopup(); return this.oldOnSubmit(); };
		}
	}
}

function cancelPopup()
{
	bShowPopup = false;
}
function showPopup()
{
	// check to see various things here
	// if the popup has already appeared it should set a cookie
	// saying so.  Check this cookie here and block if the cookie
	// exists.
	//	if (the shownpopup cookies exists...)
	//	{
	//		bShowPopup = false;
	//	}

	// check to see if the exit variable is true
	if (bShowPopup) {
		window.open('http://blah', 'Popup',
'width=549,height=316,left=100,top=100,scrollbars=no');
		// cookie setting code...
		// setCookie('SHOWN_SESSION', 'true', 0);
	}
}
</script>
 

Finally, in the <body> tag, I added an onload/onunload handler:
<body onload="initializeClicks();" onunload="showPopup();">
which would set up the stuff so that all links get the custom onclick
handler which would cancel the popup.  Then, when the page is unloaded,
it will call showPopup() which will determine whether or not to show the
popup.

Basically, this code takes all onclick and all onsubmit handlers and
appends a function called cancelPopup() to it, telling the page not to
show the popup when the page unloads.  If any link doesn't already have
an onclick handler, this will create one for it.

If there are links on the page that you want to handle differently (you
want a person clicking on that link to get a popup), then you can either
assign those links a special class and parse that class info in
initializeClicks() and omit adding the onclick handler, or you can parse
out the .href parameter of the link and determine if the href matches a
pattern that should cause a popup.

HTH,

Roger



More information about the thelist mailing list