OdeToCode IC Logo

jQuery 1.9.x and ASP.NET MVC 4 Applications

Wednesday, February 27, 2013

If you update or restore your NuGet packages in an ASP.NET MVC 4 application, you've probably discovered the breaking changes in jQuery 1.9.x.

From the upgrade guide:

jQuery 1.9 removes or modifies several APIs that behaved inconsistently or inefficiently in the past. The majority of these changes have been foreshadowed by their deprecation in previous versions of jQuery, particularly 1.7 and 1.8.

Depending on what other packages you've upgraded or installed (like jQuery UI, jQuery Mobile), you'll see various runtime errors in a JavaScript debugger. Here is the error thrown by the jquery.unobtrusive-ajax.js file authored by Microsoft to form a bridge between HTML helpers and JavaScript code.

Object has no method live

Update: If the only script problems you are seeing are in the jQuery.unobtrusive scripts, make sure to use the NuGet update tab to update all your unobtrusive script packages from Microsoft. Unfortunately, many packages with a dependency on jQuery say they require jQuery 1.4.4 to jQuery 2.0, so  with the breaking changes in 1.9 you’ll need to manually update packages. 

There are many other possible solutions if you can't change your own code right away, but here are two easy ones.

1) install-package jQuery.Migrate.

jQuery Migrate is a script you'll want to include in pages immediately after jQuery. The migration script will add back some of the features removed from jQuery 1.9 (like the live function). The development version also provides trace messages that will highlight the deprecated APIs in use so you can update your own code to the newer APIs.

jQuery Migrate trace messages

2) install-package jQuery –Version 1.8.3

You can always revert to a previous version of jQuery if you don't want to use the additional migration script.

Hope that helps!