tags:

views:

56

answers:

3

I would like to put jQuery at the bottom of the body tag but at the sametime, I have ASP.NET user controls that could potentially use jQuery methods (ie: $()) placed above where jQuery's script is referenced. When this happens, an error is thrown reading that $() is not defined or does not exist.

How can I defer the execution of the javascript in my ASP.NET user controls so it'll give the browser time to initialize jQuery? Or if there's another solution to my problem, im open to it.

A: 

Hi,

Put JQuery in the header tag, and it should work.

yoda
+4  A: 

You cannot use jQuery before it is defined - there is simply no way to do this as the JavaScript interpreter in the browser needs to know what you are referencing.

You ought to reference jQuery in the <head> of your page and use the $(document).ready() function to execute JavaScript when the page has loaded.

Andrew Hare
+1  A: 

You could accomplish what you're looking for if-and-only-if those UserControls call jQuery (including any calls to $(document).ready) after you load jQuery. Unless you're able to change the implementation of your UserControls you will need to place the jQuery reference in the <head> element as other answers recommend.

Ken Browning