Injecting jQuery into a web site

Posted by

This is really useful if you need to hack around with a site that doesn’t use jQuery. In my case, the web interface of a crappy Chinese IP camera which did its recording scheduling by making you click every 15 minute block in the week that you want to record.

Full credit should be given to http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet.

Code which you can put into your browser’s debugging console is as follows:

(function getScript(url){
    var script=document.createElement('script');
    script.src=url;

    var head=document.getElementsByTagName('head')[0],
    done=false;

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

            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
        }
    };

    head.appendChild(script);
})('http://code.jquery.com/jquery.min.js');

Leave a Reply

Your email address will not be published. Required fields are marked *