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!

gravatar Brett Wednesday, February 27, 2013
Does the latest package update from Microsoft (feb 18th, Microsoft jQuery Unobtrusive Validation 2.0.30116.0) still not resolve these issues?
gravatar Scott Allen Wednesday, February 27, 2013
Yes, sorry for the confusion. Updated the post.
gravatar Tian van Heerden Friday, March 1, 2013
The update read together with the following paragraph now implies that installing jquery-migrate will fix the unobtrusive validation error: it won't.
gravatar Scott Allen Friday, March 1, 2013
Tian: You updated Microsoft.jQuery.Unobtrusive.Ajax to => 2.0.30116 ? It is using .on instead of .live, which is the recommendation.
gravatar Tian van Heerden Friday, March 1, 2013
I guess it was my turn to be ambiguous. I meant that *just* installing jquery.migrate will not fix the unobtrusive validation error - it is also necessary to update unobtrusive validation package. Your "update" note recommends updating the unobtrusive package but it is followed by text from your first draft which says "There are many other possible solutions if you can't change your own code right away [...e.g.] install jquery.migrate" To my mind this could be interpreted to mean that installing jquery.migrate is an ALTERNATIVE to updating unobtrusive - which we all agree it isn't. Was just trying to help others avoid confusion. Huge fan of your work, by the way. Thanks for what you contribute to the community.
gravatar Scott Allen Friday, March 1, 2013
@Tian: No worries, and thanks. I should have been much more careful in the post, my update, and the comment!
Comments are closed.
Pluralsight Courses
What JavaScript Developers Should Know About ECMAScript 2015
The Podcast!