I am doing cross domain call.
This is taking more time(some ms or 1s).
so below that code is executing.
var xdr;
if (window.XDomainRequest) // Check whether the browser supports XDR.
{
xdr = new XDomainRequest(); // Create a new XDR object.
if (xdr) {
xdr.onload = function () {
var data = $.parseJSON(xdr.responseText);
AddData(data, link);
};
xdr.open("post", urlSearch);
xdr.send();
}
else {
alert('Server Error!! Try Later.');
}
}
else {
alert("Not IE 8");
}
I want to do that synchronous.
So that after that call finished below code will execute
please help
Thanks in advance.
Just to clarify, since some “JavaScript” programmers tend to be actually jQuery programmers ( and I doubt that they can even be that, I’m talking about comments ).
Back to the topic. After few minutes of googling it seems that XDomainRequest object does not support synchronous calls.
And this is actually good. What you don’t know is that synchronous AJAX ( note that synchronous AJAX is a contradiction on its own – first A stands for Asynchronous ) is the root of all evil in browsers. Why? Because JavaScript is single-threaded, so when user waits for a request to finish he can’t do anything – entire page is blocked ( and in some worse cases, like IE8 I believe, entire browser is blocked ).
So what should you do? You should do what every good JavaScript programmer does and write your code in an asynchronous way. For example:
If you have more code that depends on
xdr, then simply add it toonloadhandler ( you can wrap it in a separate function to make it easier to read ).