Investigating the jQuery Source

As a web developer I always have to stay up on the newest and greatest technologies. On that note I always try to expand my Javascripting abilites and now consequently my jQuery knowledge. A great book I read called jQuery: Novice to Ninja suggested that one of the best ways to gain insight and to really understand the Javascript framework you use is to read the actual source. Right after I started reading the source I found a great video-blog article with Paul Irish titled “10 Things I Learned from the jQuery Source“. I found his video to be fun, insightful and very useful. He also takes the jQuery getScript function and makes it library independent, making it useful for bookmarklets and other projects where you can’t/shouldn’t call jQuery. Since I took the time to type up his script and I think it is very useful I am posting it here so others can copy and paste it instead of re-typing it again.

getScript function:

function getScript(url, callback){

var head = document.documentElement,
script = document.createElement("script");

script.src = url;

var done = false;

//Attach handlers for all browsers
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")){
done = true;

callback();

//handle memory leak in IE
script.onload = script.onreadystatechange = null;
if (head && script.parentNode) {
head.removeChild(script);
}
}
};

//use insertBefore instead of appendChild to circumvent an IE6 bug.
//this arises when a base node is used
head.insertBefore(script,head.firstChild);

//we handle everything using the script element injection
return 'scriptinjected!';
}

getScript test function:

getScript('http://www.cornify.com/js/cornify.js',function(){
var times = [42, 28, 75, 50, 62];
times = times[Math.floor(Math.random()*times.length)];

while(--times)
cornify_add();
})

I hope people find this code useful as I think it certainly is!

And just for fun here is the assembled “Cornify” bookmarklet:

javascript: function getScript(url, callback){var head = document.documentElement,script = document.createElement("script"); script.src = url; var done = false; script.onload = script.onreadystatechange = function(){if(!done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")){done = true; callback(); script.onload = script.onreadystatechange = null; if (head && script.parentNode) {head.removeChild(script);}}}; head.insertBefore(script,head.firstChild); return undefined;} getScript('http://www.cornify.com/js/cornify.js',function(){var times = [42, 28, 75, 50, 62]; times = times[Math.floor(Math.random()*times.length)]; while(--times) cornify_add();})

This entry was posted in life. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted February 25, 2013 at 10:34 pm | Permalink

    Hello! I could have sworn I’ve been to this website before but after browsing through some of the post I realized it’s
    new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back often!

  2. Posted March 19, 2013 at 5:32 am | Permalink

    I have read some excellent stuff here. Definitely price
    bookmarking for revisiting. I surprise how a
    lot effort you set to create any such excellent informative site.

  3. Posted March 19, 2013 at 10:35 am | Permalink

    Highly descriptive post, I loved that a lot. Will there be a part 2?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>