Writing a javascript bookmarklet is a great way to add functionality to a web page that you don’t control. But if you use jQuery, wouldn’t it be neat to be able to use that in your bookmarklet? You can do this by loading jQuery in and adding it to the document:
var jQueryLib = document.createElement("script");
jQueryLib.src = "http://code.jquery.com/jquery-1.6.1.min.js";
document.body.appendChild(jQueryLib);
It may take the browser a second or two to load jQuery, so I’d recommend putting a delay in before your code continues. You can do this using the setTimeout function:
/* Give the browser a short while to load jQuery */
var myCode = setTimeout(function() {
/* your code here */
},1000);
The timeout is in milliseconds, so the above code puts in a one second delay before continuing execution.
2 Responses to Using jQuery in bookmarklets