PDA

View Full Version : Pop-up window event on last page visited


robsta
09-06-2003, 12:55 AM
Hi there!

I'm looking for a script to handle a pop-up window event when a visitor exits my site.

I'm posting a survey page and would like to have a "tell us how we're doing" page pop-up before they're allowed to exit the site.

Is this something that can be handled with JavaScript? Cookies?

I know that when a person chooses to exit a site, it could be a tad annoying to get this pop-up request to fill out a mini-survey.

Anyone have any better suggestions how to go about this?

Thanks!
Robsta

Mr J
09-06-2003, 11:56 AM
You could use the onunload event in a specific page to open a popup.

If you use a frameset the onunload could be placed in the frameset page.

Failing that you'd have to go server-side, maybe


<SCRIPT language=JavaScript>

function goodbye(){
open("yourpage.htm"); <!--Place your path and file name here-->
}
</SCRIPT>


Add onunload=goodbye() to the opening <Body> tag

<BODY onunload=goodbye()>

robsta
09-06-2003, 05:15 PM
Does the onUnload event automatically detect that the user has chosen to exit my domain for another? Or do I only add this script to selected pages - in which case, would I need cookies to keep the pop-up from opening if they've already filled out the survey?

If I went server side as you suggested, what would that entail? - other than some form coding in php, I'm new to server side programming.

All feedback appreciated.

Thanks -
Robsta
:)

robsta
09-07-2003, 12:27 AM
Hi Jeff!

On further reflection:

Problem: I'm not sure which page is going to be the one that a visitor will actually exit the site from - so I'd have to make an educated guess based on my stats, and add the onUnload script to those pages only (and a cookie to make sure that I only present the survey to the user once).

Is it possible to handle the event by modifying the script so that the OnUnload event is present on each page, but only functions when the domain server location switches? Can this be done in JavaScript, or is this a server-side issue?

Is there a better way?

Here's the code I'm using:

[CODE]
<script language="JavaScript">
function survey(){
var newWin = window.open('info/surveyPopup.html','MyWindow987','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollba rs=yes,resizable=no,width=260,height=270,left=95px,top=15px');newWin.focus();return false;
}
</script>
</head>
<body onunload="survey()">
[CODE]

Whaddya think?
Robsta
:)

Mr J
09-07-2003, 08:03 AM
Hi Robsta.

I use to have a goodbye popup on my site when I first started my own website and I had the same problem you are having, which page to put it on.

I decided then that I would go with a frame layout because this allowed me to put the popup in the frameset page which would only unload when my site was left.

I haven't yet got into PHP or ASP yet so I cannot help you with a server-side solution but here is my solution from a few years ago.

Create a frameset with a hidden frame.


<FRAMESET FRAMEBORDER=no BORDER=0 COLS="0,*">
<FRAME SCROLLING="no" SRC="scripts.htm" NAME="left" NORESIZE>
<FRAME SCROLLING="AUTO" SRC="page1.htm" NAME="main" NORESIZE>
</FRAMESET>

The frameset created only shows 1 frame "main".
Your current index page is loaded into this frame and your site should perform as normal.

A page is that contains the script is loaded into the hidden left frame .


The following script is a PopOnce cookie script.

This cookie only lets the popup be shown once until the time limit has expired



<SCRIPT language=JavaScript>
<!--
var expDays = 31; // number of days the cookie should last

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value,expires) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function survey() {
var count = GetCookie('poponce');
if (count == null) {
count++;
SetCookie('poponce', count, exp);

// Action to take
dothis()
}
}

function dothis(){
var windowprops = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=260,hei ght=270,left=95px,top=15px";
var newWin = window.open("info/surveyPopup. html", "MyWindow987", windowprops); // Use to load a page
newWin.focus()
}
// -->
</SCRIPT>

<BODY OnLoad="survey()">

robsta
09-08-2003, 07:26 PM
Hi jeff -

Thanks for the script (especially the pop once cookie - you saved me a lot of work there).

I'll play around with the hidden frameset. I set up the entire site using CSS positioning and don't have a clue how the two will marry. I'll let you know how it works out.

I'm looking into a server-side solution also, which I'll gladly share with you if I get something resolved.

Thanks a bunch for all your help!!

Robsta
:thumbsup:

robsta
09-09-2003, 03:13 AM
Hi Jeff -

Is it possible to alter the script so that it works on <body Unload="survey()">?

Right now, when I change the event handler to <body Unload="survey()">, it loads the popup on load, and unload.

The page that I'm running the script on is a form submission page, so I need the popup to happen after the form's been submitted rather than before.

Otherwise, what you did works wonderfully - I made a couple of changes - I dropped the frameset (I wasn't sure how that might affect my search engine ranking!) and linked the script as a.js file. I hope you don't mind :D

Any and all help appreciated.

All the best,
Robsta
:)

Mr J
09-09-2003, 07:46 AM
No problem, if it works as you want it to on "onunload" then do it that way

:thumbsup: