jQuery Best Practices
jQuery extends the browser’s JavaScript interface, often providing thin wrappers for functions you can perform in plain JavaScript with a similar amount of code.
When jQuery is wrapping a native feature, it is doing so to provide two benefits:
- It normalizes differences between browser JavaScript interfaces, adapting them into a single jQuery one
- It provides an alternative paradigm for interacting with the DOM (one that is more functional in nature, and less procedural or imperative than the native JavaScript methods)
This can be thought of as normalizing the differences across browser types and providing a different way of approaching the problems jQuery solves.
When jQuery is providing its own features or implementations, it is doing so to either:
- Polyfill or normalize features or concepts that are may not be implemented natively, particularly in older browsers.
- To fill in gaps of functionality that do not exist at all in the underlying interface
This can be thought of as normalizing between versions of browsers and providing entirely new functionality, unavailable natively in any of them.
Having a solid understanding of when jQuery is wrapping a native feature, and when it is providing its own implementation, is the key to understanding how to write performant jQuery.
The following is not intended as a comprehensive guide, but more a useful overview and grouping of the functionality jQuery can provide. Please consult the official jQuery API docs for more information - particularly on what methods may be available for the version of jQuery you’re using.
jQuery objects
jQuery object are usually instantiated with CSS selectors (or similar), that match 0 or more DOM elements.
They’re an example of the Composite design pattern, whereby a single object may be interacted with (i.e. provide the same interface) as a collection of objects, and vice versa.
Once instantiated, they provide 3 major pieces of functionality:
- Getters, which allow accessing attribute and property values of the first element matched.
- Setters, which allow setting attribute and property values of all the elements matched.
- Providing a scope to then perform further filtering or DOM traversal
jQuery object are sets
Although you often interact with jQuery objects as if they are single DOM elements, it’s important to think of them as always representing a set, rather than a single value (even if you know that set contains a single element).
Operate on the set rather than the elements
Perform actions on the set, rather than attempting to iterate over the elements individually:
1 2 3 4 5 6 7 |
|
Use set operators instead of logic ones
It’s important to understand where applying functions to sets removes the need for explicit logic. For example:
1 2 3 4 5 6 7 8 9 10 11 |
|
Use the union set operator rather than repeated logic
You can union two sets of elements, (or add an individual element to an existing set) where you want to then work on them as a common set. This often simplifies logic and removes duplication.
1
|
|
Handle (and use) empty sets
Because the jQuery objects may match no elements (an empty set), you may need to consider when this happens in your code. Empty sets return undefined
for getters, and perform a no-operation for setters.
Some expressions are simpler by explicitly passing an empty set; You can do so with an empty selector:
1
|
|
Selectors and the DOM
Prefer ids
Where possible, use an id selector to find the first element with that id (it’s invalid to have a HTML document with multiple elements with the same id). This delegates to native browser functions which are optimised and the fastest way to locate an element.
jQuery objects are eagerly evaluated, so ever if you don’t end up calling any methods on the jQuery object, it still performs the (relative expensive) operation of finding the matching selectors.
1 2 3 4 5 6 7 8 |
|
Prefer to avoid jQuery filter extensions
If using an id to find an element is not possible (or you need to match more than one element), stick to plain CSS selectors, if you can. These are much more efficient than the syntactic sugar jQuery provides.
In general, check the docs before using any of the following, to understand if there is a more performant CSS-only alternative:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Use performant CSS selectors
A full discussion of what CSS selectors are the most performant is outside the scope of this article, but some general heuristics are as follows (each comes at the price of less specificity, which may or may not work with your document - consider your use case before applying):
These are heuristics only; the size of their effect (and even if that effect is net positive or negative) will depend on your document, the version of jQuery, web browser and device you’re using. Always measure performance yourself and avoid premature optimisation.
It may be helpful when reading the following to know that CSS selectors are applied right-to-left (i.e. starting with leaf nodes and then walking the DOM back up to the root).
Filtering based on hierarchy is expensive (particularly when it’s not an immediate child match). Try to filter only on tag, class and attribute type where possible:
1 2 3 4 5 6 7 8 9 10 11 |
|
Use selectors with as few components as possible (it’s less to match):
1 2 3 4 5 |
|
Use less permissive operators where possible:
1 2 3 4 5 |
|
Perform filtering in CSS where possible
Perform as much filtering as you can in CSS to avoid instantiating more jQuery objects in memory than you need, and performing the equivalent in the relatively slow JavaScript:
1 2 3 4 5 |
|
Search only the part of the document you need
Where possible, search only the descendants of a known ancestor, rather than the entire document:
1 2 3 4 5 6 7 8 9 10 |
|
Reuse references to ancestors
Often you will already have a ancestor element in scope. Use it to restrict further searching:
1 2 3 4 5 6 7 |
|
Prefer selection over traversal
You can think of traversal as jumping to a convenient nearby element and then performing a smaller hop to the element you’re actually interested in. Selection, however, is jumping straight to the element you’re interested in.
jQuery provides methods for both, and they both have their place (traversal is particularly useful for moving between sibling elements, for example), but always carefully consider selection before resorting to traversal:
1 2 3 4 5 6 7 8 9 10 11 |
|
Getters and Setters
jQuery getters and setters are both called on sets. However, the getter returns the matching attribute of only the first element, while setters set the value of the attributes of all matching elements. To get the attributes of all matched elements, you must use each()
or map()
:
1 2 3 4 5 6 7 8 9 10 |
|
Setter methods are designed to be chained, and return the jQuery object, allowing several method calls to be chained in somewhat of a Builder design pattern. Getters, on the other hand, return the value of the attribute you’re requesting.
If you want to set an attribute based on logic at call-time (or the element’s current value), you can pass a function to setter methods to return the desired new value, which is called once for each HTML element in the matching set. The function is passed the element’s index in the set as the first argument, and its current value as the second. It is also bound to the current element, so this
refers to it.
jQuery treats HTML elements as having four different attribute types, which you must get and set differently (particularly in later versions of jQuery):
- Properties: Values that affect the dynamic state of a DOM element without changing the serialized HTML attribute. They often record the result of the user interacting with the HTML document, and are oriented around recording some sort of state.
- Value: These are a special type of property, that apply to some elements (particularly form elements) which have values associated with them.
- Attributes: Can be thought of as everything else, but are more specifically attributes of the element, and are part of the HTML document.
- Data attributes: These are a special type of pseudo-attribute that contain arbitrary custom data associated with the element, bootstrapped from the values stored as part of the HTML document as
data-*
HTML attributes (although they can be added to or modified in jQuery afterwards without being necessary written back to the DOM).
Examples of properties:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
To get and set properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
To get and set values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
To get and set attributes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
To get and set data:
- When getting and setting values as data, it is converted to a string and then back to a JavaScript object, unless that conversion result in rounding errors (floats, for example, are left as strings).
1 2 3 4 5 6 7 8 9 10 11 |
|
Styling
Working with classes
A common way to apply styling is to manipulate a DOM element’s classes (thereby changing the style rules that match the element).
To query whether an element currently has a class:
1
|
|
Modifying an element’s classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Calculated values, dimensions and positions
Sometimes it’s not possible to achieve the styling or functionality you need through CSS alone; this is where jQuery’s dimensions methods come in handy. They normalize the differences between quantities reported by browsers into a unified interface.
CSS values
To query the CSS values of elements (property names are camelcased):
1 2 3 4 |
|
Dimensions
jQuery’s dimension methods return unit-less pixel values, which may be not be whole numbers.
- They return the same values, regardless of the CSS
box-sizing
- The values are incorrect if the user has zoomed in or out
- The values may be incorrect if the parent element is hidden (jQuery temporarily shows, measures and then hides the element - which can have a large impact on performance)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Positions
Offset
An element’s offset is the absolute coordinate of it’s border box relative to the document
(as an object with left
and top
attributes).
- Does not support hidden elements
- Does not include the margin of the
html
tag, if appropriate - Values are incorrect if the page is zoomed in or out
1 2 3 4 5 6 7 8 9 |
|
Relative positions
To get the position of an element’s padding box to its parent:
1 2 3 4 |
|
Scroll position
An element’s scroll position is the number of pixels hidden from view, above the scrollable area.
- If the element is not scrolled, the value is 0
1 2 3 4 5 6 7 |
|
DOM Mutation
JQuery provides the ability to not only filter and locate elements in the DOM, but also to mutate it.
Parsing HTML strings into HTML elements
Parse a HTML string into an array of DOM nodes:
- Doesn’t remove any leading or trailing whitespace (use
$.trim()
first, for that)
1
|
|
Use for an iframe:
- Default for before jQuery 3.0:
context
is the currentdocument
(or if you passundefined
ornull
); - Default for jQuery 3.0: A new
document
is used. (scripts
now don’t automatically run on the current document and you have the chance to iterate over the HTML and remove anything harmful before it’s added into the currentdocument
.)
1
|
|
Parse and keep the script
tags:
- Must explicitly opt in
- Not true of most other methods in jQuery - they’ll keep
script
tags in and likely execute them
1
|
|
Replacing Elements
Outside replace (replace entirely)
Use replaceAll
when you already have the replacement as a jQuery object, and replaceWith
when you already have the target to be replaced, as a jQuery object:
- When
replaceWith
is passed a jQuery object that matches a single element, it has the effect of moving that element from its current position in the DOM.
1 2 3 4 5 6 7 |
|
Inside replace (replace contents only)
1 2 3 4 5 |
|
Removing elements
Removing an element’s parent
1 2 3 4 5 |
|
Outside remove (remove entirely)
1 2 3 4 5 6 |
|
Inside remove (remove contents)
1 2 |
|
Appending elements
Outside append (append siblings)
Use insertAfter
when you already have the HTML to append as a jQuery object, and after
when you already have the target to append to, as a jQuery object:
- When
after
is passed a jQuery object that matches a single element, it has the effect of moving that element from its current position in the DOM.
1 2 3 4 5 6 7 |
|
Inside append (append children)
Use appendTo
when you already have the HTML to append as a jQuery object, and append
when you already have the target to append to, as a jQuery object:
- When
append
is passed a jQuery object that matches a single element, it has the effect of moving that element from its current position in the DOM.
1 2 3 4 5 6 7 |
|
Prepending elements
Outside prepend (prepend siblings)
Use insertBefore
when you already have the HTML to prepend as a jQuery object, and before
when you already have the target to prepend to, as a jQuery object:
- When
before
is passed a jQuery object that matches a single element, it has the effect of moving that element from its current position in the DOM.
1 2 3 4 5 6 7 |
|
Inside prepend (prepend children)
Use prependTo
when you already have the HTML to prepend as a jQuery object, and prepend
when you already have the target to prepend to, as a jQuery object:
- When
prepend
is passed a jQuery object that matches a single element, it has the effect of moving that element from its current position in the DOM.
1 2 3 4 5 6 7 |
|
Wrapping elements
Outside wrap (Adding parent)
To wrap matching elements in a selector, HTML string, element or jQuery Object (first matching element is used):
1 2 3 4 5 6 7 8 9 |
|
Inside wrap (Adding children to wrap all current children)
1 2 |
|
Animation
If you need to make animated changes, (which can’t be achieved with CSS or manipulating the DOM), jQuery provides a series of functions, each with the same signature:
1 2 3 4 5 6 |
|
These functions work by animating width
, height
or opacity
(sometimes all 3) over a duration period, and then setting the display
value to none
(when hiding), or to restore it its previous value (when showing).
To animate all 3 properties:
1 2 3 4 5 6 7 8 9 |
|
To animate the opacity
property (causing the elements to appear to fade in and out):
1 2 3 4 5 6 7 |
|
To animate the height
property (causing the content below to appear to slide up):
1 2 3 |
|
These methods are actually convenience wrappers around a much more powerful animation function that allows you to animate any numeric style property:
1 2 3 |
|
The full list of animation options are detailed here.
Events and user interaction
The event object
To understand how jQuery handles events, it’s important to have a familiarity with the jQuery event object. It’s a superset (or extension) of those provided by browsers natively, with differences in behaviour normalized away. The native browser event is always available on the jQuery one as event.originalEvent
.
It has the following properties (values are populated, depending on the type of event being triggered):
Elements
|
|
target
|
DOM element that initiated the event.
|
currentTarget
|
Current DOM element within the event bubbling phase.
|
delegateTarget
|
Element where the currently-called jQuery event handler was attached
$( ".box" ).on( "click", "button", function( event ) {
$( event.delegateTarget
).css( "background-color", "red" );
});
|
Propagation
|
|
isDefaultPrevented()
|
Whether event.preventDefault() was
ever called on this event object.
|
isImmediatePropagationStopped()
|
Whether event.stopImmediatePropagation() was
ever called on this event object.
|
isPropagationStopped()
|
Whether event.stopPropagation()
was ever called on this event object.
|
Keyboard
|
|
metaKey
|
Whether the META key was pressed when the event fired.
|
which
|
For key or mouse events, this property indicates the specific key or button that was
pressed.
|
Mouse
|
|
pageX
|
mouse position relative to the left edge of the document.
|
pageY
|
mouse position relative to the top edge of the document.
|
relatedTarget
|
Other DOM element involved in the event, if any.
|
General
|
|
timeStamp
|
Time in milliseconds
|
type
|
Type of event
|
Custom & Message pasing
|
|
data
|
Optional object of data passed to an event method when the current executing handler is
bound.
|
result
|
Last value returned by an event handler that was triggered by this event, unless the value
was undefined.
$( "button" ).click(function( event ) {
return "hey";
});
$( "button" ).click(function( event ) {
$( "p" ).html( event.result );
});
|
namespace
|
Namespace specified when the event was triggered.
|
Browser events
Document ready
To attach behaviour as soon as the DOM becomes safe to manipulate:
- Good time to perform tasks needed before the user views or interacts with the page, such as adding event handlers and initializing plugins
- Most browsers provide the equivalent
DOMContentLoaded
event, however if you bind handlers to this event after it’s already been triggered, the handlers are never invoked. jQuery’s method will call any handlers bound after the event, immediately, so you don’t have to worry about your setup code being skipped.
1 2 3 |
|
To wait for all assets (including images) on the page to load:
1 2 3 |
|
Window resizing
1 2 3 4 5 6 7 8 9 |
|
Window and elements scroll
1 2 3 4 5 6 7 8 9 |
|
Binding event handlers
In order to bind events to elements, they must exist in the DOM at the time. This is why you should always wait for the document to be ready before attempting to bind handlers:
1 2 3 |
|
You also need to bind you event handlers to elements above any content that may be modified after the initial document is ready (this includes DOM mutations you perform yourself, or content you may load via AJAX), and use event delegation to call the correct handler.
1 2 3 4 |
|
In generally, event handlers (particularly delegated ones) should be bound to as small and as few a parts of the document as possible - particularly for events that are triggered many times a second like scroll
and mousemove
. You want to avoid listening to events for other parts of the document that you don’t end up handling.
Event bubbling and event delegation
Browser events bubble, or propagate, from the deepest, innermost element in the document where they occur (the event target), up to the root document
. jQuery uses this fact (and actually simulates it in some cases) to provide event delegation: whereby you can bind an event handler for an element to its ancestor, by providing a selector to match events that originate from the event target.
Using event delegation makes it easier to bind event handlers to elements that may change after the DOM has loaded (see above); it also makes for more performant code in circumstances where binding an event handler to a single ancestor saves you having to do so for many individual elements:
1 2 3 4 |
|
Binding Type
|
Descendant Selector used
|
Value of this is element
|
Handler is called when event
|
|
Directly occurs on target
|
Occurs on descendants
|
|||
Direct
|
No
|
Where the handler was attached
|
Yes
|
Yes
|
Delegated
|
Yes
|
Matching selector (not where event occurred)
|
No
|
Yes
|
Preventing propagation and default behaviour
Sometimes it’s preferable to prevent event propagation (so an ancestor element doesn’t try to handle an event a descendant has already handled, for example). This is prevented with .stopPropagation()
.
Some elements also have a default behaviour the browser invokes when an event occurs (e.g. a link is followed when clicked on). This can be prevented by calling .preventDefault()
on the event object.
Preventing the default behaviour and stopping propagation will not prevent any other jQuery handlers bound to that element from running. Use .spotImmediatePropagation()
to prevent those from running.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Events that don’t bubble
focus
and blur
events are specified by the W3C to not bubble. However, jQuery defines custom cross-browser focusin
and focusout
events that do bubble. When focus
and blur
are used to attach delegated event handlers, jQuery maps them internally to focusin
and focusout
.
load
, scroll
, and error
events also do not bubble.
In IE 8 and lower, the paste
and reset
events do not bubble.
Manually triggering events
You can manually simulate the triggering of events, complete with event bubbling.
- For plain objects and DOM objects other than
window
, if a triggered event name matches the name of a property on the object, jQuery will attempt to invoke the property as a method (if no event handler callsevent.preventDefault()
). - If an event name matches the name of a property on the object, prefixed by
on
, jQuery will attempt to invoke that property as a method.
1 2 |
|
If you don’t want to trigger any native property with a mtching name, or have event bubbling:
- Triggers any handlers attached through jQuery and any native methods attached to the element with the same name, prefixed by
on
. - Events do not bubble up the DOM hierarchy
- Returns result of the handler, rather than the jQuery object for further chaining
- Won’t call
eventName()
on the element (e.gtriggerHandler('submit')
won’t call.submit()
)
1 2 |
|
Removing event handlers
Automatically unbind a handler after its first invocation:
- Takes same arguments as
on()
1 2 3 |
|
Removing handlers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Custom events
If a handler is bound to a string value that is not the name of a native DOM event, then it is considered a custom event. These are never called by the browser, but may be triggered manually.
Namespaced events allow triggering or unbinding events without affecting the native ones (or other custom ones). If a event type string contains a period, it’s a namespaced event (they should contain only letters and digits).
1 2 3 4 5 6 7 |
|
Passing custom data to event handlers
Passing data at bind-time:
1 2 3 4 5 |
|
Passing at trigger-time:
1 2 3 4 5 |
|
Event shorthands
jQuery provides a number of shorthands for common events. They’re equivalent to calling the longer .on(eventName, ...)
form, and accept the same arguments (aside from the event name being defined for you).
1
|
|
Mouse events
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Mouse event objects have the following values for which
:
1
For left button2
For right button3
For middle button
Keyboard events
Keyboard events are sent to the element that is currently in focus (focusable elements vary between browsers).
Global keyboard events can be captured by attaching to document
.
In order to determine which key was the subject of an event, jQuery normalizes this information into the which
event property, which contains a key code. For keydown and keyup events, its value does not change for uppercase letters. For the keypress event, it’s value is different for uppercase letters. E.g. a lowercase “a” will be reported as 65
by keydown and keyup, but as 97
by keypress. An uppercase “A” is reported as 65
by all events.
|
When
|
Triggered by modifier & non-printing keys
|
.which code indicates
|
Covered by specification
|
|
Keydown
|
User presses key (every time OS repeats key)
|
Yes
|
key pressed
|
Yes
|
|
Keypress
|
Browser registers keyboard input
|
No
|
Character entered
|
No
|
|
Keyup
|
User releases key (every time OS repeats key)
|
Yes
|
key pressed
|
Yes
|
1 2 3 |
|
Focus events
Historically browsers limited focusable elements to some form elements, but modern browsers extend this to other elements that have a tabindex
property set.
1 2 3 4 5 6 7 8 9 10 11 |
|
Change events
The change events are limited to input
, textarea
and select
elements. The event is fired immediately when the user makes a selection for select boxes, checkboxes and radio buttons; the event is deffered for other element types until it loses focus.
Changing the value using JavaScript (e.g. using .val()
) doesn’t fire the event.
1
|
|
Selection events
Selection can occur for input
of type text
and textarea
elements, when the user selects some of the element’s input. The method for retrieving the selected text varies between browsers, but there are jQuery plugins that help normalize it.
1
|
|
Form submission events
Submit events only occur on form
elements and occur when clicking a input
or button
of type submit
or image
, or by pressing Enter when certain form elements are focused (although this varies between browsers). The event occurs before the actual submission, so you have a chance to prevent it.
1
|
|
AJAX
Serializing data for requests
1 2 3 4 5 6 |
|
It’s possible serialize the values from a form as a JSON string. Although jQuery can act on selected individual form controls, it’s easier so select the form
tag itself. jQuery will then use the W3C rules for successful controls to determine which elements to include. Namely:
- Submit button values are not serialized when the form was not submitted using a button
- Form elements must have a
name
attribute to be included - Checkbox and radio button values are included only if they are checked
- Form field data is not serialized
1 2 3 |
|
Similarly, for serializing form data as URL-encode strings:
1 2 3 4 5 |
|
jqXHR and Promise objects
All AJAX methods return jqXHR
objects, which implement the Promise interface:
|
(Deprecated) name (removed in jQuery 3.0)
|
Equivalent to callback
|
done(function( data, textStatus, jqXHR ) {});
|
success
|
success
|
fail(function( jqXHR, textStatus, errorThrown ) {});
|
error
|
error
|
always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { })
|
complete
|
complete
|
then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {});
|
|
success and error
|
jqXHR objects can be used for setting handlers at the the time the object is instantiated, or at a later point.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
AJAX shorthands
jQuery provides a number of convenience methods that wrap the more generalized ajax
method:
Method
|
Equivalent
|
$.get( url,
data, success, dataType)
|
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
$.get( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
|
$.getJSON(url,
data, success);
|
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
|
$.getScript(url,
success);
|
$.ajax({
url: url,
dataType: "script",
success: success
});
|
$.post( url,
data, success, dataType)
|
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
|
Loading HTML into the DOM
jQuery also provides a helper for loading HTML documents over the AJAX and mounting their response into the DOM.
- Ajax request will not be sent if no element is matched by the selector
1 2 3 4 5 6 7 8 9 |
|
AJAX options
This table explains the complete set of AJAX options used with the generalised $.ajax()
function:
Settings
|
Default
|
Description
|
||
Request Headers
|
||||
accepts
|
Depends on dataType
|
Sets the Accept header
|
||
contentType
|
application/x-www-form-urlencoded; charset=UTF-8
|
As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header
For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded,
multipart/form-data, or text/plain will trigger the
browser to send a preflight OPTIONS
request to the server
|
||
mimeType
|
|
mime type to override the XHR mime type
|
||
headers
|
|
X-Requested-With: XMLHttpRequest
is always added, but default value can be changed
Values can also then be overridden in beforeSend
function
|
||
Request Behaviour
|
||||
method and type
|
GET
|
HTTP method to use for the request
|
||
url
|
Current page
|
URL to send request to
|
||
async
|
true
|
If you need synchronous requests, set this option to false
|
||
cache
|
true
false for dataType 'script' and 'jsonp'
|
By default, requests are always issued, but the browser may serve results out of its cache
Set to false to force requested
pages not to be cached by the browser
|
||
crossDomain
|
false for same-domain requests,
true for cross-domain requests
|
If you wish to force a crossDomain request (such as JSONP) on the same domain
|
||
isLocal
|
Depends on protocol
|
Allow the current environment to be recognized as "local," (e.g. the filesystem)
|
||
xhr
|
Callback for creating the XMLHttpRequest object.
|
|||
xhrFields
|
Object of fieldName-fieldValue pairs to set on the native XHR object.
$.ajax({
url: a_cross_domain_url,
xhrFields: {
withCredentials: true
}
});
|
|||
Authentication
|
||||
username
|
|
Username to be used with XMLHttpRequest in response to an HTTP
access authentication request.
|
||
password
|
|
Password to be used with XMLHttpRequest in response to an HTTP
access authentication request.
|
||
Script requests
|
||||
script
|
|
Object with additional attributes to be used in a "script" or "jsonp" request.
|
||
scriptCharset
|
|
Sets the charset attribute on the script tag used in the request
|
||
Request content (body or query parameters)
|
||||
data
|
|
Data to be sent to the server
POST data will always be transmitted to the server using UTF-8 charset, per the W3C
XMLHTTPRequest standard.
In requests with dataType: "json" or dataType: "jsonp", if the string contains a double question mark (??) anywhere in the URL or a single question mark (?) in the query string, it is replaced with a value generated by jQuery that is unique for each copy of the library on the page (e.g. jQuery21406515378922229067_1479880736745).
|
||
processData
|
true
|
By default, data passed in to the data option as anything other than a
string will be processed and transformed into a query string, fitting to the default
content-type "application/x-www-form-urlencoded”
|
||
traditional
|
|
true if you wish to use the
traditional style of param serialization (see serializaiton section above)
|
||
Response behaviour
|
||||
contents
|
|
Object of string or regular expression keys that determine how to parse response
|
||
converters
|
|
dataType-to-dataType converters:
Default:
{
"* text": window.String,
"text html": true,
"text json": jQuery.parseJSON,
"text xml": jQuery.parseXML
}
Use transports when converters and
prefilters aren’t flexible enough.
|
||
dataType
|
Intelligent Guess (xml, json, script, or html)
|
Type of data that you're expecting back from the server
|
||
Data type
|
Preprocessing
|
Desscription
|
||
“text"
|
None
Passed to success as jqXHR .responseText (String)
|
A plain text string.
|
||
"html"
|
Returns HTML as plain text; included script
tags are evaluated when inserted in the DOM.
|
|||
"json"
|
jQuery.parseJSON
Passed to success as jqXHR .responseJSON (Object)
|
Evaluates the response as JSON and returns a JavaScript object.
|
||
"script"
|
Execute the JavaScript that is received from the server
Passed to success as (String)
|
Evaluates the response as JavaScript and returns it as plain text. Disables caching by
appending a query string parameter, _=[TIMESTAMP],
to the URL unless the cache option
is set to true.
|
||
"jsonp"
|
Automatically append a query string parameter of (by default) callback=? to the URL
Server should return valid JavaScript that passes the JSON response into the callback
function.
jQuery will execute the returned JavaScript, calling the JSONP callback function
Passes JSON object containing response to success
|
Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL
to specify the callback.
|
||
"xml"
|
jQuery.parseXML
Passed to success as jqXHR .responseXML (XMLDocument)
|
Returns XML document that can be processed via jQuery.
|
||
multiple, space-separated values
|
|
As of jQuery 1.5, jQuery can convert a dataType
from what it received in the Content-Type header to what you
require.
|
||
statusCode
|
|
Object of numeric HTTP codes and functions to be called when the response has the
corresponding code
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
|
||
dataFilter
|
|
Function to be used to handle the raw response data
Function( String data, String type ) => Anything
|
||
timeout
|
|
Timeout (in milliseconds) for the request
|
||
ifModified
|
false
|
Successful only if the response has changed since the last request
|
||
Callbacks
|
||||
context
|
Set the value of this for callbacks
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$( this ).addClass(
"done" );
});
|
|||
global
|
Whether to trigger the global Ajax event handlers for this request (see below)
|
|||
jsonp and jsonpCallback
|
Override the callback function name in a JSONP request (string or boolean)
{ jsonp:’onJSONPLoad’ } // result in 'onJSONPLoad=?' passed to the server
Specify the callback function name for a JSONP request
{ jsonpCallback:’foo’ }
|
|||
beforeSend
|
Can be used to modify the jqXHR object before it is sent
Function( jqXHR jqXHR, PlainObject settings )
|
|||
success
|
Function to be called if the request succeeds
Function( Anything data, String textStatus, jqXHR jqXHR )
|
|||
error
|
Function to be called if the request fails
Function( jqXHR jqXHR, String textStatus, String errorThrown )
|
|||
complete
|
When the request finishes (after success and error callbacks are executed)
Function( jqXHR jqXHR, String textStatus )
|
Other utilities
If you are already using jQuery, and need to support older browsers, you can often save the overhead of a polyfill or the headache of detecting browser support by using the implementations that jQuery provides.
If you don’t need to support older browsers, need a polyfill for another reason, or are using some sort of JavaScript transpilation build pipeline, then going with the more native JavaScript functions may be a better idea.
Arrays
Use jQuery’s methods iterating and filtering arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Objects
Similarly, jQuery provides helpers for objects:
1 2 3 4 5 6 7 |
|