开发者

Executing JavaScript after Ajax-call

I want to execute javascript for creating calendar using "calendarDateInput.js". Is it possible to execute javascript after an ajax call开发者_C百科 page using ajax tabs ?

I am not using any of the ajax libraries, only direct ajax call.

Here I want to call the function in an ajax returned page like DateInput("smsDate",true, "YYYY-MM-DD");


Usually this is done by Callback functions. If you use libraries like jquery they usually provide hooks for callbacks.

check out jQuery.ajax() for doing the ajax call and then onsucess you can run additional javascript code...

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.post/

var jqxhr = $.ajax({ url: "example.php" })
.success(function() { alert("success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

I hope this is what you were asking for... otherwise please add some additional information to your question...

if you would not like to work with libraries like jquery ....

you have to check for the request status of you ajax call....

req = new XMLHttpRequest();

and then you have to check the response for following values

 // IF completed
if (req.readyState == 4) {

// Server HTTP Code if (req.status == 200) {

but jquery did all the work for you...


yes. for non jQuery:

var httpRequest = new XMLHttpRequest(); //your AJAX object
httpRequest.onreadystatechange = AJAXhandler; //your ajax handler function

function AJAXhandler() {
  if (httpRequest.readyState === 4) { //if success
    //process..
    //"YOU CAN CALL OTHER FUNCTIONS HERE"
  }
}


When using jQuery or normal javascript, callbacks are THE way things like this are handled. For example, using jQuery:

$.ajax({
   type: 'POST',
   url: 'PathTOMyUrl.html',
   success: function(data)
   {
       // This Callback will be invoked on successful call
   },
   error: function(data)
   {
       // This callback will be invoked on an error
   }
});

The callbacks can then contain code you find useful. In normal javascript, you would attach to the onreadystatechanged event on your AjaxRequest. You can find more information here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜