最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Executing JavaScript after Ajax-call - Stack Overflow

programmeradmin13浏览0评论

I want to execute javascript for creating calendar using "calendarDateInput.js". Is it possible to execute javascript after an ajax call 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");

I want to execute javascript for creating calendar using "calendarDateInput.js". Is it possible to execute javascript after an ajax call 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");

Share Improve this question edited Apr 1, 2011 at 18:00 Ben asked Apr 1, 2011 at 17:40 BenBen 4,4723 gold badges25 silver badges20 bronze badges 1
  • 1 How are you making your Ajax call? I could give an example with jQuery... – Mayo Commented Apr 1, 2011 at 17:42
Add a ment  | 

3 Answers 3

Reset to default 2

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./jQuery.ajax/ http://api.jquery./jQuery.post/

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

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 pleted
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)

  1. 暂无评论