views:

67

answers:

3

I'm loading user control through jQuery in my asp.net page.

User control contains JavaScript files, while loading the user control all my js load at one time which are dependent on each other and they tend to give error while all file load at one time. So I want that my JavaScript file to load synchronously one by one , as one file get completely loaded than next file should start loading .

Is there any way to set synchronously mode in JavaScript? or any JavaScript to set this? Any pointer or suggestion would be really helpful.

+1  A: 

JavaScript files always load synchronously. In fact, JavaScript always runs synchronously because it is single threaded.

My guess is that you need to work out which order to include the files so that it runs properly. You can use the window.onload event to run script once all of the JavaScript and images have been loaded.

Douglas
JavaScript files are *downloaded* asynchronously if they are added to the DOM using *document.createElement()*. This means a smaller file that's added after a larger file could be parsed and executed first.
Andy E
@Andy E: Hmm, I didn't know that about using createElement, cheers. I'll watch out for it if I do dynamic loading of script files.
Douglas
@Douglas thanks for the reply ! If i try $.getScript() will it be asynchronously or synchronously ??
Monu
+2  A: 

JS files are loaded in the order you put them in your HTML code.

For example,

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"&gt;&lt;/script&gt;

You always need to load jQuery before jQuery UI (or UI will not be recognize since it uses the jQuery $ shortcut) so you must put the line with jQuery before the one with jQuery UI into your HTML. And when your page is fully loaded, js will start thanks to window.onload, $(document).ready(function(){}); for jQuery or via the first command it will encounter.

Chouchenos
+1  A: 

You should use jQuery.load() to load only a HTML fragment and not a full page with the scripts. jQuery use DOM structure of the loaded document to modify the DOM structure of the corresponding part of your page (controls).

In general you can use jQuery.ajax to load a script, but I recommend you to use the simplified form jQuery.getScript() instead. jQuery.getScript() can be used to load a JavaScript file from the server using a GET HTTP request and then execute it. Using success event handler you can do some action after the script are loaded.

Oleg
@Oleg i m altering my code and trying to do this now hope it help me :) http://colourgray.wordpress.com/2008/09/22/jquery-loading-external-javascript-files-using-getscript/
Monu
@Monu: I suppose you can load script without usage of **async: false**. Just use `success` event handler to control the moment when the script is **already loaded** and you can use it. You can cascade many `$.ajax` or `$.getScript()` requests inside of `success` event handlers.
Oleg
@Oleg cool thanks !!! btw async: false worked for me but i will try your suggestion too !! thanks again !!
Monu