Stuff that makes your head explode...

by Codewiz51 August 30, 2010 19:52

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. Cool

MVC, Ajax, jQuery...

by Codewiz51 March 11, 2010 18:22

I used to gripe about having to update my Boost C++ libraries so often, but these web/.Net things seem to morph by the day.  I am trying to get up to speed on modern web design techniques and so far, I've updated my components twice in the last two weeks.

More...

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant


Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

All comments are moderated for content.

© Copyright 2008-2010