I've been experimenting with jQuery and AJAX in VS 2008. Today, I ran into an issue that nearly made my head explode. I have some javascript code that I use to detect if a user has a popup blocker installed. This is an internal application and it makes use of popups to deliver information. The code isn't strictly correct, but it works with the various versions of IE that we need to support.
Under the head tag, the following code block tests if a window can be opened from code:
<script type="text/JavaScript" language="JavaScript">
var mine = window.open('', '', 'width=1,height=1,left=0,top=0,scrollbars=no');
if (mine) {
var popUpsBlocked = false;
mine.close();
}
else
var popUpsBlocked = true;
</script>
Just after the </body> tag the following code alerts the user to the state of their pop up blocker:
<script type="text/JavaScript" language="JavaScript">
if (popUpsBlocked)
alert('Please disable popup blocker software when using this web site.');
else
alert('No popup blocker.');
</script>
I added a script tag to the <head> tag of the html to load jQuery:
<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"/>
Low and behold, my javascript code was borked with an exception that the var popUpsBlocked was not declared!
After much experimenting, I was able to add popUpsBlocked as a global variable as follows:
<script type="text/JavaScript" language="JavaScript">
var popUpsBlocked = false;
</script>
The finished aspx page is coded as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"/>
<script type="text/JavaScript" language="JavaScript">
var popUpsBlocked = false;
</script>
<script type="text/JavaScript" language="JavaScript">
var mine = window.open('', '', 'width=1,height=1,left=0,top=0,scrollbars=no');
if (mine) {
popUpsBlocked = false;
mine.close();
}
else
popUpsBlocked = true;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
<script type="text/JavaScript" language="JavaScript">
if (popUpsBlocked)
alert('Please disable popup blocker software when using this web site.');
else
alert('No popup blocker.');
</script>
</html>
I guess the moral is, be prepared to experience something that frustrates the heck out of you everday. 