Javascript Popups

Zippeh

Limp Gawd
Joined
Sep 26, 2002
Messages
377
Before anyone says anything, I agree that they are evil and I should be killed horrifically for attempting this work, but my boss has told me to do it, so doing it I am!

Aaaaanyway!

What I need to do is prompt a user when they leave our site, to fill in our questionnaire. At present I have a Javascript confirm box opening asking if they want to fill it in, and if they choose "OK" then it opens a popup, whilst the original window goes to where they wanted, thus minimally disrupting the individuals browsing.

Everything works fine when they click and external link, or change the address bar and leave the site that way. But when they close the window, the Javascript popup comes up ok, but even if they choose "OK", the window closes and no popup appears :(

Anyone have a clue why??

Thanks in advance!
 
Here's my code :)

Code:
function openpopup() {

	topLeftX = 0;
	topLeftY = 0;
	pickRandom = false;
	ratioAsked = 10;
	optionList = '';

        var strCwestiwn;
	var iaith = new String(get_cookie('iaith'));
	iaith = iaith.substr(iaith.length-1, iaith.length);

	if (iaith == "1") {
      strCwestiwn = 'Would you like to fill in our questionnaire?';
	  surveyName = "/form.htm";	//Can be a full URL or a reference name
	} else {
      strCwestiwn = 'A fuasech yn hoffi llenwi ein holiadur i mewn?';
	  surveyName = "/ffurflen.htm";	//Can be a full URL or a reference name
   	}

	if ((parseInt(Math.random() * ratioAsked)) == 1 || pickRandom == false)	{
		var blnDangosHol = confirm(strCwestiwn);
		if (blnDangosHol) {
			var width=800;
			if (parseFloat(navigator.appVersion)>=4)
				width=screen.width;
			
			if (width<700)
				pageSize = 'width=550,height=420';	//Screen resolution < 800x600
			else
				pageSize = 'width=590,height=512';	//Screen resolution 800x600 or above	
			if (navigator.appName =="Netscape") {
				window.open(surveyName,'',pageSize+',screenX='+topLeftX+',screenY='+topLeftY+optionList);
			} else {
				window.open(surveyName,'',pageSize+',left='+topLeftX+',top='+topLeftY+optionList);
			}
			return true;		    
		}
	}
}

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
	    if (offset != -1) { // if cookie exists
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;
		returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function loadpopup() {
  //***	USER SETTINGS ***//
  dropCookie = true;
  //***	END OF USER SETTINGS ***//
	
  if (get_cookie('popped')=='') {
    if (openpopup() && dropCookie) {
      var expireDate = new Date();
      expireDate.setFullYear(expireDate.getFullYear()+1);
      //document.cookie="popped=yes;expires=" + expireDate.toGMTString();
    }
  }
}

//Add an onclick event to each link
var a = document.getElementsByTagName('a');
for (var i=0;i<a.length;i++) {
  a[i].onclick = gwirioSafle;
}

//Checks to see if it is an external link thats clicked
function gwirioSafle(strCyfeiriad) {
  var strSafle = 'test.whatever.com';
  if (strCyfeiriad) strCyfeiriad = strCyfeiriad.target;
  else strCyfeiriad = window.event.srcElement;
  strCyfeiriad = new String(strCyfeiriad);
  var intIndecsSafle = strCyfeiriad.indexOf(strSafle, 0);
  if (intIndecsSafle == -1) {
	loadpopup();
  } 
}

//Checks to see whether the window is being closed <--- I just copied this from somewhere else and it appears to work fine ;)
var UserClicked=false;
document.onkeydown=spy;
document.onmousedown=spy;

function spy() {
  UserClicked=true;
  setTimeout("UserClicked=false",2000);
}

function popup() {
  if(!UserClicked) {
    loadpopup();
  }       
}

The "popup()" function is called onUnload. When adding alerts around the "window.open" bits, it is clear that the code goes through these parts, but it just doesn't the window :(
 
I couldn't replicate the problem. Using (large parts of) your code gave reliable popups when closing the browser. For the heck of it, you might try running your popup using onbeforeunload instead of onunload. Beyond that, need a link to see how your real page differs from my test.
 
I've created a small test page which has the problem I explained earlier:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/2001/REC-xhtml11-20010531/DTD/xhtml11-flat.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cy">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="" type="text/css" />

</head>
<body onunload="popup()">
<a href="popup_test.html">Internal link</a><br /><br />
<a href="http://www.google.com">External link</a><br />
<!-- Javascript file goes here -->
<script src="/js/popup.js" type="text/javascript"></script>
<!-- Javascript file ends here -->
</body>
</html>

When I close the window, the confirm box comes up, then when I click "OK" the popup opens for a split second, then closes again when the main window actually closes.
 
Tried it on different box, and got the evil results you described. It would seem that this is a XPsp2 thing.

I know of no work-around -- and since this is an intentional 'enhancentment' to IE, I doubt there is a solution.
 
Right well I'm actually getting somewhere now! It's magically started working!!

The only problem I'm having now is how to read the target href when an image link is clicked. When it's a normal link the target attribute works, but when it's an image link, the target brings back some crap that's not the link.

Any ideas?
 
Back
Top