Search code examples
jqueryasp.net-mvc-4internet-explorer-8bundling-and-minificationjquery-2.0

Checking for jQuery using ASP.NET Bundling fails with IE8


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?


Solution

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