views:

147

answers:

2

I'm downloading JQuery asynchronously:

function addScript(url) {
    var script = document.createElement('script');
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
}
addScript('jquery.js');

// non-jquery code ...

// jquery specific code like: $()...

As such - how do I call my JQuery specific code once JQuery is loaded (because since I'm downloading my JavaScript asynch - it's not blocking, which is good, but is trying to execute my JQuery specific code before JQuery has been loaded).

A: 
dclowd9901
I actually want to call a jquery function that already exists inline the HTML page. So it would be more like *script_object.onLoad(my_jquery_related_code())');*
Teddyk
@dclowd9901, I get an error, "script_object.onLoad is not a function"
Teddyk
Yeah, I was afraid that'd happen. I think onLoad's only reserved for `window` or `document`. Updated answer with antoher option...
dclowd9901
You can `return script` in your function, and `.onload = my_jquery_related_code` with the result similar to what you had originally :)
Nick Craver
The call_back_function will be called before the script is loaded.
Tomas
Good to know. Thanks for the head's up. Like I said, it was throwing spaghetti at a wall.
dclowd9901
Also call_back_function.call should be call_back_function(). The use of the call method misses parentheses and is meaningless here.
Tomas
A: 

You can host a copy of the jquery file yourself. Then you can add a call to the callback function at the bottom of jquery.js:

/* jquery code goes here ... */

my_onload_callback();
Tomas