Skip navigation

Monthly Archives: April 2010


Synchronous Request in Ajax means javascript will stop processing your program until a result has been obtained from the server. While the request is processing the browser is effectively frozen. The browser treats the call like an alert box or a prompt box only it doesn’t wait for input from the user but on input by the remote server. 99.99 of the time the response is quick and fast enough so that there isn’t any problems.

Synchronous request in ajax???? ajax itself means asynchronous javascript and xml. But why the hell do you need synchronous request from ajax???

Before going that a brief basics…

Asynchronous ajax will initiate a request to the server and then return control to the browser. After sometime you can still manage the result return from server(means server responds, timedout, error, etc.).

Synchronous request in ajax means javascript will stop processing your program until a result has been obtained from the server. Until it is processing the browser is frozen.

Now back to topic…

When this asynchronous behaviour seems good, still we need synchronous behaviour at certain situations. Say for example if a user needs to signout after check out something online. In this case it is better to synchronize ajax call, otherwise before server return, user may navigate to other page which is not good.

Right. How to sjax (synchronous javascript and xml)?

In open method of ajax object, set thrid parameter as false. thats it.

Here goes an example

function getAsset(url) {
  if (window.XMLHttpRequest) {
    xhr=new XMLHttpRequest();
  } else {
    xhr=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xhr) {
    xhr.open("GET", url, false);
    xhr.send(null);
    return xhr.responseText;
  } else {
    return false;
  }
}
var asset = getAsset("http://domain.com/file.ext");

JavaScript have some odd behaviour on object property names. If you know this for first time, you will be shocked. Programmers from other language background will be surprised to know this kind of JavaScript behaviour.

// declare a variable and create a new object with property name and value raja

var myO = {name:"raja"};
alert(myO.name);  //raja

Your shocks starts here…

myO.if = "use reserved word";
alert(myO.if);  //use of reserved word

myO["if else if && || *^ "] = "you can use anything";
alert(myO["if else if && || *^ "]);  //you can use anything

myO[""] = "Surprise";  // no error. even empty is allowed
alert(myO[""]);  // Surprise

Similar article written by mathias

Follow

Get every new post delivered to your Inbox.

Join 150 other followers