I would like to understand how jquery handles context. I have this code:
var formHtml;
jQuery.get("form2.htm", function (data) {
formHtml = data;
});
alert(formHtml);
What it does, it gets data from the form2.html page and I am setting the formHtml variable to that data so that I can use it somewhere else but when I alert that I get undefined. Is there something I am missing?
jQuery.get()is an asynchronous call. jQuery.get() uses AJAX to fetch the value from the server side. That means the execution of the javascript statements will continue after sending the request to server without waiting for the response from the server.AJAX stands for
Asynchronous JavaScript and XML.When response from the server comes back the callback method registered with the
postmethod gets called.So by the time the server request comes back the
alertstatement would have already executed. Since you have declared the global variableformHtmland not initialized it, its value isundefinedthat is what you are getting in your alert.