最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Executing JavaScript after Ajax-call - Stack Overflow

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far