jQuery JavaScript Library
  • jQuery
  • Plugins
  • UI
  • Meetups
  • Forum
  • Blog
  • About
  • Donate
  • jQuery 1.4 Event Sponsors
  • Subscribe: RSS, Email, or Twitter
14 Days of jQuery

An online event marking the release of jQuery 1.4. To celebrate, we're unveiling 14 releases over a period of 14 days.

  • Sponsors
  • spacer
  • spacer
  • spacer
  • spacer
  • spacer
  • spacer
  • spacer
  • spacer
  • spacer
  • Sponsor jQuery! Download the Prospectus!
 
Twitter
  • Loading...

Follow on Twitter

Subscribe

Enter your email address:

Delivered by FeedBurner

jQuery 1.4

  • A
  • B
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Thursday, January 14, 2010

  • jQuery 1.4 Released - Full Release Notes
  • jQuery 1.4 Live Q&A

jQuery 1.4 Released – Full Release Notes


14 Days of jQuery: 15 Days of Prizes. Submit your cool uses of jQuery, win a laptop and hosting!

In celebration of jQuery’s 4th birthday, the jQuery team is pleased to release the latest major release of the jQuery JavaScript library! A lot of coding, testing, and documenting has gone into this release, and we’re really quite proud of it.

I want to personally thank Brandon Aaron, Ben Alman, Louis-Rémi Babe, Ariel Flesler, Paul Irish, Robert Katić, Yehuda Katz, Dave Methvin, Justin Meyer, Karl Swedberg, and Aaron Quint who put a lot of work into fixing bugs and getting the release out the door.

Downloading

As usual, we provide two copies of jQuery, one minified (we now use the Google Closure Compiler as the default minifier) and one uncompressed (for debugging or reading).

NOTE: jQuery 1.4.1 has already been released. Please use that instead of the 1.4 release.

  • jQuery Minified (23kb Gzipped)
  • jQuery Regular (154kb)

Additionally, Google has provided us with a copy of jQuery hosted on their servers. This copy of jQuery is automatically minified and gzipped – and served from Google’s fast edge cache servers.

  • ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js

You can feel free to include the above URL directly into your site and you will get the full performance benefits of a quickly-loading jQuery.

With jQuery 1.4 we attempted to minimize any large upgrade hassles – maintaining the signatures of all public functions. That being said, please read through the list of potentially-breaking changes to be aware of what might cause problems in your applications.

Features

Below is an overview of all the changes and functionality added to jQuery 1.4. Additionally all of the changes have been documented in the jQuery 1.4 docs.

Performance Overhaul of Popular Methods

Many of the most popular and commonly used jQuery methods have seen a significant rewrite in jQuery 1.4. When analyzing the code base we found that we were able to make some significant performance gains by comparing jQuery against itself: Seeing how many internal function calls were being made and to work to reduce the complexity of the code base.

spacer
View the cropped chart.

In jQuery 1.4 we’ve significantly reduced the complexity of the most popular methods in jQuery. The full performance details can be found below.

Easy Setter Functions

For a while now, you’ve been able to pass a function into .attr() and the return value of that function is set into the appropriate attribute. This functionalilty has now been extended into all setter methods: .css(), .attr(), .val(), .html(), .text(), .append(), .prepend(), .before(), .after(), .replaceWith(), .wrap(), .wrapInner(), .offset(), .addClass(), .removeClass(), and .toggleClass().

Addtionally, for the following options, the current value of the item is passed into the function as the second argument: .css(), .attr(), .val(), .html(), .text(), .append(), .prepend(), .offset(), .addClass(), .removeClass(), and .toggleClass().

This enables code like:

// find all ampersands in A's and wrap with a span
$('a').html(function(i,html){
  return html.replace(/&amp;/gi,'<span class="amp">&amp;</span>');
});
 
// Add some information to the title of the anchors
$('a[target]').attr("title", function(i,title){
  return title + " (Opens in External Window)";
});

Ajax

Nested param serialization (jQuery.param() Documentation, Commit 1, Commit 2)

jQuery 1.4 adds support for nested param serialization in jQuery.param, using the approach popularized by PHP, and supported by Ruby on Rails. For instance, {foo: ["bar", "baz"]} will be serialized as “foo[]=bar&foo[]=baz”.

In jQuery 1.3, {foo: ["bar", "baz"]} was serialized as “foo=bar&foo=baz”. However, there was no way to encode a single-element Array using this approach. If you need the old behavior, you can turn it back on by setting the traditional Ajax setting (globally via jQuery.ajaxSettings.traditional or on a case-by-case basis via the traditional flag).

There are three ways to enable the traditional way of serialization:

// Enables for all serialization
jQuery.ajaxSettings.traditional = true;
 
// Enables for a single serialization
jQuery.param( stuff, true );
 
// Enables for a single Ajax requeset
$.ajax({ data: stuff, traditional: true });

More information: jQuery.param() Documentation, jQuery.ajax() Documentation, Commit, Code

JSON and script types auto-detected by content-type (jQuery.ajax Documentation, Commit 1, Commit 2)

If the response to an Ajax request is returned with a JSON mime type (application/json), the dataType defaults to “json” (if no dataType is specified). Additionally, if the response to an Ajax request is returned with a JavaScript mime type (text/javascript or application/x-javascript) , the dataType defaults to “script” (if no dataType is specified), causing the script to automatically execute.

Etag support has been added (jQuery.ajax() Documentation, Commit)

By default, jQuery ignores the Last-Modified header for Ajax requests, preferring to make request and ignore the browser cache. Specifying ifModified: true causes jQuery to use the browser cache if available. jQuery 1.4 will also send the If-None-Match header (for ETag support) if you specify ifModified.

Strict JSON parsing, using native JSON.parse (jQuery.ajax() Documentation, Commit 1, Commit 2, Commit 3)

jQuery 1.3 and earlier used JavaScript’s eval to evaluate incoming JSON. jQuery 1.4 uses the native JSON parser if available. It also validates incoming JSON for validity, so malformed JSON (for instance {foo: "bar"}) will be rejected by jQuery in jQuery.getJSON and when specifying “json” as the dataType of an Ajax request.

Serialize HTML5 elements (jQuery.param() Documentation, Commit)

The new HTML5 input types (such as `datetime` and `range`) will be included when you .serialize() a form.

Context for Ajax Request (jQuery.ajax() Documentation, Commit)

You can now specify a context for an Ajax request, and all callbacks will have that context set (allowing you to simplify your code and possibly avoid having to use closures, or use other objects).

jQuery.ajax({
    url: "test.html",
    context: document.body,
    success: function(){
        jQuery(this).addClass("done");
    }
});

Success callback receives XHR object as third argument (jQuery.ajax() Documentation, Commit)

The success callback for any ajax request now receives the XMLHttpRequest object as the third argument. Previously this XHR object was only accessible as the return value for $.ajax and the like.

Explicitly set a content-type (jQuery.ajax() Documentation, Commit)

In jQuery 1.3, the contentType setting was ignored in jQuery.ajax if no data was sent. In jQuery 1.4, the contentType is always sent. This fixes an issue with some backends that used the Content-Type header to decide what kind of response to send.

Explicitly specify a JSONP callback name (jQuery.ajax Documentation, Commit)

You can now specify the name of the JSONP callback in jQuery.ajax() using the jsonpCallback option.

Avoid pre-flighting cross-domain XHR (Commit)

Cross-domain ajax (for the browsers that support it) is smoother with jQuery as preflighting is avoided by default.

jQuery.ajax() is now using onreadystatechange instead of a timer (Commit)

Ajax requests should now take fewer resources by using onreadystatechange instead of polling.

Attributes

The performance of .css() and .attr() has been improved.

spacer

The .attr() takes a function setter (.attr() Documentation)

Not only can you use a function with .attr(), but you can also use the current value of the attribute with the function.

jQuery('<img src="/img/spacer.gif"> )
.attr("alt", function(index, value) {
    return "Please, " + value;
});

.val( Function ) (.val() Documentation)

<input class="food" type="text" />
<input class="food" type="text" />
jQuery("input:text.food").hide();
 
jQuery("&lt;ul class='sortable'&gt;
 
	<li>Peanut Butter&lt;/li&gt;&lt;li&gt;Jelly&lt;/li&gt;&lt;/ul&gt;")
  .sortable()
  .bind("endsort", function() {
    $(":text.food").val(function() {
      return $("ul.sortable li:eq(" + $(this).attr("data-index")  + ")").text();
    });
  });</li>

.text() works on text and CDATA nodes (.text() Documentation, Commit)

Core

Quick Element Construction (jQuery() Documentation, Commit)

When you create a single element with the jQuery function, you can now pass in an object to add attributes and events at the same time:

jQuery("
 
<div>", {
    id: "foo",
    css: {
        height: "50px",
        width: "50px",
        color: "blue",
        backgroundColor: "#ccc"
    },
    click: function() {
       $(this).css("backgroundColor", "red");
    }
}).appendTo("body");</div>

The keys of the object are functions that will be called with each value passed as an argument.

.eq(-N), .get(-N) (.eq() Documentation, .get() Documentation, Commit)

You can now pass in negative numbers for .get() and .eq(). For example, you can select the second-to-last div or, you can access the DOM element for the same:

$("div").eq(-2);
$("div").get(-2);

New .first() and .last() methods (.first() Documentation, .last() Documentation, Commit)

For convenience, .first() and .last() are aliases of .eq(0) and .eq(-1).

New .toArray() method (.toArray() Documentation, Commit)

.get() has historically returned an Array from the jQuery set. For further clarity, you can use .toArray() to achieve the same thing in jQuery 1.4. Unlike, .get(), however, .toArray() does not take an argument.

jQuery() returns empty set (jQuery() Documentation, Commit)

In jQuery 1.3, jQuery() returned a jQuery set containing just the document. in jQuery 1.4, it returns an empty jQuery set. This can be useful for creating an empty set and adding elements to it dynamically. Note: The jQuery().ready() technique still works in 1.4 but it has been deprecated. Please use either jQuery(document).ready() or jQuery(function(){}).

jQuery(“TAG”) (Element Selector Documentation, Commit)

A faster path is used when passing in a single tag name.

jQuery(“<div>”) jQuery(“<div/>”) and jQuery(“<div></div>”) (jQuery() Documentation, Commit)

All three now use the same code path (using document.createElement), improving performance for jQuery("<div></div>"). Note that if you specify attributes, we use the browser’s native parsing (using innerHTML).

CSS

The performance of the .css() method has seen a 2x performance improvement.

spacer

The performance of the .addClass(), .removeClass(), and .hasClass() methods has seen a 3x performance improvement.

spacer

.toggleClass() can toggle multiple classes (.toggleClass() Documentation, Commit)

You can now call .toggleClass() with multiple selectors and they will all be toggled.

$("div").toggleClass("current active");

Data

.data() returns Object and .data(Object) sets the object (.data() Documentation, Commit 1, Commit 2)

It is sometimes desirable to work with the data attached to an element as a complete object. A common example would be wanting to copy the entire data from one element to another. In jQuery 1.4, .data() with no parameter returns the entire object, and .data(Object) sets the object. Keep in mind that this object includes events bound to the element, so use caution.

Data cache is no longer created if it isn’t needed (Commit 1, Commit 2, Commit 3)

jQuery uses a unique expando on DOM elements that is used to get the .data() for a particular element. jQuery now avoids creating that expando when data is looked up but no data has been added. This potentially increases performance and avoids polluting the DOM in these cases.

Effects

Per-property Easing (Per-property Easing Documentation, Commit)

In addition to being able to specify an easing function for an animation you can now specify an easing animation for individual properties. James Padolsey has more information and demos in his blog post.

$("#clickme").click(function() {
  $("div").animate({
    width: ["+=200px", "swing"],
    height: ["+=50px", "linear"],
  }, 2000, function() {
      $(this).after("
 
<div>Animation complete.</div>
 
 
");
  });
});

Events

New Method: jQuery.proxy() (jQuery.proxy() Documenation, Commit 1, Commit 2)

If you want to ensure that “this” inside a function will be permanently bound to a particular value, you can use jQuery.proxy to return a new function with that scope.

var obj = {
  name: "John",
  test: function() {
    alert( this.name );
    $("#test").unbind("click", obj.test);
  }
};
 
$("#test").click( jQuery.proxy( obj, "test" ) );

Event Multi-binding (.bind() Documentation)

You can now pass an object of many events to bind to an element.

$("div.test").bind({
  click: function(){
    $(this).addClass("active");
  },
  mouseenter: function(){
    $(this).addClass("inside");
  },
  mouseleave: function(){
    $(this).removeClass("inside");
  }
});

`change` and `submit` events normalized (Change Documentation, Submit Documentation)

The change and submit events work reliably across browsers for b

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.