PDA

View Full Version : Confirm Only When Leaving Domain + Cookies



richmurphy
03-29-2010, 10:23 PM
I have the following code written to give a user the option of participating in our survey, however, I cannot get it to only pop up when leaving the domain vs. clicking links internally. I have searched and searched and tried to implement code that I found on here among other sites without luck. I know I am just screwing up something simple so I need your help! I have also tried to implement code to set and read cookies so that it doesn't pop up excessively for a user. Although I would be ok without the cookie code.

Here is what I got:


<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>

window.onbeforeunload = function(){
location.assign('http://www.survey.com');
return "Thanks for visiting! We would appreciate if you could take the time to fill out our short survey to enhance your experience with our organization. To take our survey please hit CANCEL below. To continue without taking our survey just hit OK. Thanks again!";
}
</script>
</body>
</html>


Thanks in advance for your help. I didn't include any other code that I tried to implement as I figured it would be easier for you all to direct me in the right direction and help implement the code into mine.

richmurphy
03-30-2010, 08:36 PM
Ok. So I think I got the code for the most part. However I do have two questions. First, how can I specify a specific domain to also cause the script to not fire? For example the website that I am working on has links that for most part are all on the same domain, but then also a couple of others that are on a separate domain, but still a part of the site. The code I have checks the current domain and suppresses the script from firing. I hope that makes sense. Secondly, is there anyway to use the confirm dialog box instead of the "default" return dialog box. I don't want it to prompt the "Are you sure you want to navigate..." question. I know the code to place the confirm box in there, but it isn't passing the survey link through for the redirect. It is going to their intended site no matter if they press OK or CANCEL. Also when I add confirm to the script it causes two boxes to come up one after another. The second one is the "default" return dialog box with the word false in it.

Any help would be greatly appreciated.

Here is my code:



<script type='text/javascript'>

(function()
{
window.onbeforeunload = function()
{
if( !/\bnagged=true\b/.test( document.cookie ) )
{
document.cookie = "nagged=true";

location.assign('http://www.survey.com');


return "Thanks for visiting! We would appreciate if you could take the time to fill out our short survey to enhance your experience with our organization. To take our survey please hit CANCEL below. To continue without taking our survey just hit OK. Thanks again!";
}
}

var lnk;

for(var i in document.links)
if( (lnk = document.links[i] ).href && lnk.href.indexOf( document.domain ) > -1 )
lnk.onclick = function(){ window.onbeforeunload = null; }

})();

</script>