JQuery Ajax Events 1

It – Hardware & Software | | 5 views

Classification of Ajax Requests so far, in the first table about jQuery Ajax and Ajax requests from the $.ajax(), and $.get(().load(selector) $), $.getJSON(), $.getScript() that has introduced five Ajax methods.

From this, introduce the how to bind with several methods, the second table above from the classification of the Ajax Events.

and about the jQuery get see the links below.

  • jQuery
    • jquey.js download
  • jQuery documentation API/1.x/Ajax
  • jQuery download and installation


AJAX Events

AJAX Events to introduce from this is the event that occurs in the timing of some jQuery Ajax request.

4 Types of local and global events following 6 types there are currently, jQuery 1.3 x version to the Ajax Events. Like others before, these features are load jquery.js, you can easily use.

The type of the global Ajax Events Features
ajaxComplete AJAX requests are performed in the finish (completes).
ajaxError AJAX requests are performed in the failure (error).
ajaxSend Executed before the Ajax request will be send (send).
ajaxStart AJAX request starts to activate before the run.
ajaxStop Runs after all Ajax requests.
ajaxSuccess AJAX requests are performed with success (success).
AJAX Events (local) type Features
for the beforeSend Executed before the Ajax request is sent.
success Upon receiving successful Ajax request is executed.
error Incoming Ajax request fails to run.
complete AJAX requests regardless of the success or failure of the request when communication completes the run.

Incidentally, local event is events have had on many familiar as.AJAX method option so far.

How to use and how to set these events, verify in the next page.

How to set up and use

Must set the event DOM elements in a Web page using the Ajax Events.

jQuery to set the event at the jQuery.bind(type, data, fn), the jQuery.one(type, data, fn), jQuery.live(type, fn) such as there are methods.

Easy to say these are JavaScript addEventListener (IE’s attachEvent) will go to the cross-browser method.

If you want to apply to the DOM element is generally, bind is used much, don’t want to use one time only is generated when one, future use live.

For now, easy usage of the most basic of the bind method is, for example, like the following, to treat such events and details of each method for removing the unbind method at a later date. $ (“#my”).bind(“click”, function() {alert (“Hello is”)})
This is id name element of my click events alert (“Hello is”) and has a process called binding (tie).

Button like that as a result, when you click “Hi” that display the dialog. “my < button id =” > my </button> can verify this in the following sample.

Test1
(Click on dialogs)

So far, is a description of how to set up the event, use the bind method, try finally set the Ajax Events.

So continue to introduce Ajax Events also ordered one by one again, see using the ajaxSend performed before Ajax request outbound (send) from the previous page table for the time being here, try simple example.

It is not a sample.

When you press the button, the Ajax requests $.get method is executed, loaded from the./msg.txt element #my1 characters are written but before (before sending), was bound to the ajaxSend event within the function alert (“ajaxSend work was”) dialog appears.

test3
(set the ajaxSend event by bind)
Press <div> button. > < button id = “test1″ > test1 click </button> < p id = “my1″ </p> </div> < script type = kawarimasu here be rewritten “text/javascript” > jQuery (function ($) {binding $ “/Ajax Events ajaxSend” (“#test1″).bind(“ajaxSend”, function() {alert (“ajaxSend work was”)}) /AJAX requests run $ (“#test1″).click(function () {$.get(“./msg.txt”, function (data) {$ (“#my1″,).html(data);});}); }); Click the button </script> #test1 element works in the $.get method in the click method callback function set, not the mean beating ajaxSend directly is.

However, timing so $.get method performs the Ajax submit “before being send Ajax requests execution”, tied to the ajaxSend function within “alert (“ajaxSend was working”)” is a dialog that displays the work.

Incidentally, global event “ajaxStart, ajaxStop, ajaxComplete, ajaxError, ajaxSuccess, ajaxSend”, using such ajaxSend event type name as a method name intact out of Ajax Events and bind methods to operate.

Then, also be written as shown in the following example above is. outside is the same bind been superseded the ajaxSend method.

test4
Press <div> button (using the ajaxSend method). > < button id = “test1″ > test1 click </button> < p id = “my1″ </p> </div> < script type = kawarimasu here be rewritten “text/javascript” > jQuery (function ($) {“/Ajax Events ajaxSend” a binding $ (“#test1″)… ajaxSend (function() {alert ( “We work ajaxSend”)}) /Ajax requests run $ (“#test1″).click(function () {$.get(“./msg.txt”, function (data) {$ ().html(data); “#my1″});}); }); </script>

The next page verify differences between local and global events.

Local and global events

It is possible to classify two types of jQuery Ajax requests that local and global events.

Is local and global events to work for all requests for individual requests.

Up to now, such as “for the beforeSend, success, error, complete” local event is came out many familiar event.AJAX method.

Will events occur that are common in all Ajax requests “ajaxStart, ajaxStop, ajaxComplete, ajaxError, ajaxSuccess, ajaxSend”, in the global event, independent, each DOM element and requests.

However, can suppress in global events at individual requests not want to happen, such as to, if to ajaxSettings global properties to false.

Also, supra $.ajax() to set the global options for the method it is possible to set in the can.

For example, write the following global event does not occur in this request. $.AJAX({url: “msg.txt”, global: false, / / other options});
ajaxSettings is a jQuery Ajax behavior by default, jQuery.ajaxSetup(options) also can be set by the method and introduced it at a later date, the initial value of the global property is true. that the initial value of the global property is true will say Ajax Events are all to work initially.

Let’s try a little.

Is an experiment that will work in the Ajax requests performed by the click of a button first, ajaxSuccess, ajaxSend set to the p element with another element.
Test5
Press <div> button. > “my1 < button id =”test1″> test1 click </button> < p id =”dmy”>< /p >< div id =” “text/javascript </div> </div> < script type = here is realizing that be rewritten” > jQuery (function ($) {$ (“#dmy”).bind(“ajaxSend”, function ( ) {alert (“ajaxSend work was”)}).bind(“ajaxSuccess”, function (e, xhr) {alert (“ajaxSuccess work? n” +xhr.responseText)}); /AJAX requests run $ (“#test1″).click(function () {$.ajax({url: “./msg.txt”, success:function (data) {$ (“#my1″).html(data);}});}); }); </script>
global event started in a #test1 element request, set the #dmy element is working.

Incidentally, has received the XHR object in the second argument in bind second callback function. What global event in the format of the callback functions are set in this global event ajaxSuccess common, first argument error Ajax options, a fourth argument XHR object, the third argument is event object, the second argument is as outlined below. Give the / / argument callback function format function (event, XMLHttpRequest, ajaxOptions, thrownError) {this error / / thrownError is captured only when passed; / / DOM element}