Checking for jQuery using ASP.NET Bundling fails with IE8

2.1k Views Asked by At

I have a website using jQuery 2.0+ and would like to perform an action if jQuery hasn't been loaded. In essence, I'm checking for any browser that doesn't support jQuery 2, like IE8, 7, 6, etc.

I'm doing so like this in jquery.check.js

if (typeof window.jQuery == 'undefined') {
    //show not supported page
}

The BundleConfig is doing the bundling like this:

var b1 = new ScriptBundle("~/bundle/js").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery.check.js"
            );
        b1.Orderer = new InputBundleOrderer();
        b1.Transforms.Clear(); //this removes the mimification for debugging purposes

        bundles.Add(b1);

When on debug="true" the code works just fine. When turned to false it doesn't work. While debugging with IE8 the if statement is never reached and there are a bunch of errors.

It sounds like IE8 stops executing a JavaScript file when an error occurs and hence the code block is never reached. That is the only reason I can come up with as to why it works when debug="true" (files are loaded separately) and doesn't work when they are bundled (one single file).

Is my reasoning correct?

2

There are 2 best solutions below

0
On BEST ANSWER

As far as I know you are right. Once an error occurs the script execution stops for that file. If you really want to place them in the same file you could try to surround the jQuery declaration with a try/catch/finaly block (not sure it's a good ideea :| )

Also if you are using a .min.js file for production I believe you should not include that in the bundel.

5
On

Why don't you use something like modernizr to do these tests for you? That's what it's designed to do. You can even have it load the correct version of jQuery for the browser. Although a simpler choice would be to just use conditional comments.

<!--[if lt IE 9]>  
  <script src="jquery-1.9.0.js"></script>  
<![endif]-->  
<!--[if (gte IE 9) | (!IE)]><!-->  
  <script src="jquery-2.0.0.js"></script>  
<!--<![endif]-->  

I'm not sure why you would "not support" older browsers, since jQuery 1.9+ is feature compatible jQuery 2.x, you just have to load the right one for the right browser.

And why are you doing transforms.Clear()? debug mode automatically removes minification..