3

I'm making a bookmarklet and using jQuery for it (with noConflict). I need to wait for jQuery to load, to execute all the jQuery code.

I know I can check with typeof $ for jQuery, but I'm actually more looking for an event handler. Right now I'm just using setTimeout with a delay of 1s, because jQuery is proberly loaded then. I feel this is not a good solution. It's not clean code and relies on jQuery to load in 1s.

Is there any other way to afford this?

2
  • Are you dynamically appending jQuery to the DOM in the bookmarklet? Commented Aug 15, 2011 at 13:15
  • Yep. I'm doing that via appendChild. Commented Aug 15, 2011 at 13:15

5 Answers 5

5

Just for the records, completeness and for those that didn't know: Without an event handler, a good alternative would be to poll for jQuery every X amount of time. Ex:

function is_jquery_here(){
    setTimeout(function(){
      if(window.jQuery){
         my_jquery_code_here();
      } else {
        is_jquery_here();
      }
    }, 300);
}
is_jquery_here();
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know who downvoted this, but it's a working solution.
no me but i'd, I'm personally always against any "timeout function" usage anywhere in any occasion]
3

Since you say you're appending it dynamically, you could make use of onload:

var elem = document.createElement('script');
elem.onload = function() {
    // script is loaded, you can now do things with jQuery
};
elem.src = 'path to jquery';
document.getElementsByTagName('head')[0].appendChild(elem);

10 Comments

Ah, this looks good and seems to work! :) Thanks. I wasn't aware of that event handler.
@dotweb: Cheers. Just to make it sure, it's advisable to put onload before setting src. Otherwise, the script might have loaded before you set the onload event (if it's a small script or loaded very fast), and the handler will not execute anymore.
Thanks! I actually put the onload after the src. Now it's the other way around! :)
@pimvdb, setting the src attribute does not start the script loading. the script element will only begin being loaded when it is added to the document and the currently running script has finished. The appendChild line adds the script element to the document, so when the script has finished executing, the script element will load. There is no issue on which order which attribute is added.
@zzzzBov: I messed it up with Image. Thanks for clarifying!
|
2

Not every browser supports .onload for script elements, so if you need cross-browser compatibility, use onreadystatechange as well:

function load( src, callback )
{
  var s,r;
  s = document.createElement('script');
  s.type = 'text/javascript';
  s.src = src;
  s.onload = s.onreadystatechange = function(){
    if ( !r && ( !this.readyState || this.readyState == 'complete' ) )
    {
      r = 1;
      callback();
    }
  };
  document.body.appendChild(s);
}

Comments

0

If you have jQuery script in the page and you just want to make sure its loaded first, then I'd highly recommend that one

window.onload = function() {
  //.....your jquery code....
};

Comments

-5

Michael is right. You could also use this shorthand

$(function() {
    // Code for on docload here
});

1 Comment

He is not. What if jQuery is not loaded before this is executed? Everything will fail. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.