views:

282

answers:

2

I have the following function in a much larger script that loads translations from a php file:

function loadLanguages(language){
 var data = new Object();
 data.language = language;
 var dataString = $.toJSON(data);
 var langSt = $.ajax({
  url: "/VID/ajax/loadtranslations.php",
  data: ({data: dataString}),
  async: false
 }).responseText;
 var languageArr = $.evalJSON(langSt);
 return languageArr;
}

Works in FF, but in IE7 and IE8 the browser will hang.. When I comment out the ajax call in the function IE doesn't hang. If I set it to async: true the function doesn't work anymore, but the browsers will not hang. Only if I set async to false, IE will hang. I'm a bit puzzled why!

+3  A: 

You should never use async: false; it will always hang the browser until the request finishes.

This is a necessary consequence of the fact that Javascript is single-threaded, and will never change.

SLaks
It doesn't *hang* the browser, it just makes it completely nonresponsive during the request. When the request completes, the browser becomes responsive again. Agree with the fundamental point, though: Don't do that. :-)
T.J. Crowder
*"...that Javascript is single-threaded, and will never change."* Actually, it will: http://www.w3.org/TR/workers/
T.J. Crowder
@T.J.: I'm aware of that. However, it will not cause `async: false` to not hang. (Unless you run it in a worker)
SLaks
@Slaks: Exactly, it'll block (not hang) the worker thread, not the UI thread. But my point is that JavaScript on browsers *won't* be single-threaded forever. In fact, it's not *today*, not on Chrome at least.
T.J. Crowder
Problem is not that it hangs during the request, the problem is that it will keep hanging. IE will just stop working..
Gijserman
That would happen if the request times out. Does it ever un-hang?
SLaks
It doesn't unhang, it basically crashes, there is no warning that a script is taking too long to respond or anything..
Gijserman
Also, request shouldn't time out. It takes 250 ms in FireFox.. It's a request to a file on the same server too
Gijserman
A: 
T.J. Crowder
Well, sice it's a local request it shouldn't take too long to load, so the user's experience shouldn't be too poor (firefox does the request in 250 ms). But IE just crashes entirely, it doesn't stop for 250 ms or even 10 seconds..
Gijserman
@Gijserman: Sorry, completely misread that. Updated.
T.J. Crowder
@T.J. Crowder: Thanks for the help, tried it, but IE still hangs..
Gijserman
@Gijserman: I can't see any reason for it other than an IE bug. You'll need to identify *where* it hangs. In the `ajax` call? In the `toJSON` or `evalJSON`? You'll probably need the Script Debugger (or VS.Net if you have it) to figure this one out. Good luck.
T.J. Crowder