XMPP JavaScript Library READ MORE

Calling JavaScript function after AJAX Call

JavaScript, Sachin Puri, 2012-10-24 22:32:03

This is a common problem for any JavaScript developer. If you are also facing this problem then i have a solution for you.

The solution for this is Callback functions.

In JavaScript we can pass function name as parameter to any function.


In JavaScript given below i am using two functions:

1.  ajax(String url,String callback_function_name) - this is actual AJAX function which will do the desired AJAX task for you

2.  postAJAX() - this is function which will be called after AJAX function is completed.


Example JavaScript Code

var output=null;

function ajax(url,callback){
    var req=new XMLHttpRequest();

        if(req.readyState==4 && req.status){


function postAJAX(){



In following JS ajax() function will complete it's task and it will save the output in "output" variable after completing it's task it will call 'callback' function which is postAJAX in our current example. postAJAX will alert the output (you can do whatever you want in this function).



This ajax() function will not work in Internet Explorer, but you can write IE specific AJAX code to make it IE compatable. Please use Mozilla or Chrome to run this example code.

That's it enjoy the coding :-)

Add Your Comment
    Yes! I want to receive all comments by email

No Comments Posted Yet. Be the first one to post comment