Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created February 8, 2011 16:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/816746 to your computer and use it in GitHub Desktop.
Save cowboy/816746 to your computer and use it in GitHub Desktop.
Experimenting with loading widgets that are dependent on jQuery...
/*!
* jQuery Widget Bootstrap - v0.3 - 02/11/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function(jq_ver, jq_min, fn) {
var window = this,
doc = document,
scripts = doc.getElementsByTagName('script'),
parent = scripts[0].parentNode,
script = scripts[ scripts.length - 1 ],
jqScript,
opts,
// IE version test https://gist.github.com/527683
ie = (function() {
var v = 3,
elem = doc.createElement('b'),
all = elem.getElementsByTagName('i');
while (
elem.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : 0;
})();
// Eval options-as-script-contents.
try {
opts = new Function( 'return ' + script.innerHTML.replace( /^\s+/, '' ) )();
} catch( e ) {
opts = null;
}
// Create a script element.
jqScript = doc.createElement( 'script' );
jqScript.async = 'async';
jqScript.src = 'http://ajax.googleapis.com/ajax/libs/jquery/' + jq_ver + '/jquery' + ( jq_min ? '.min' : '' ) + '.js';
// Per http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html
if ( ie ) {
jqScript.event = 'onclick';
// Get a unique id.
jqScript.id = jqScript.htmlFor = (function() {
var id, i = 0;
do {
id = 's' + +new Date() + i++;
} while ( doc.getElementById(id) )
return id;
})();
}
// When the script is loaded, remove it, execute jQuery.noConflict( true )
// on the newly-loaded jQuery (thus reverting any previous version to its
// original state), and call the callback with the newly-loaded jQuery.
jqScript.onload = jqScript.onreadystatechange = function() {
var readystate = jqScript.readyState;
if ( !readystate || /loaded|complete/.test( readystate ) ) {
if ( ie ) {
try {
jqScript.onclick();
} catch( e ) {}
}
// Execute callback in the context of the script tage, passing in jQuery
// and any options parsed from the script innerHTML.
fn.call( script, window.jQuery.noConflict( true ), opts );
// Remove script element.
jqScript.parentNode && parent.removeChild( jqScript );
// Clean up references / handle memory leak in IE.
jqScript = jqScript.onload = jqScript.onreadystatechange = null;
}
};
// Append script to the DOM.
parent.insertBefore( jqScript, parent.firstChild );
})(
// Version of jQuery to load from CDN.
'1.4.4',
// Minified? (always set to true in production)
false,
// Your code (`this` is the script element).
function( $, opts ) {
console.log( $.fn.jquery, opts );
$(this).before( '= replacement html =' ).remove();
});
(function(a,b,c){var d=this,e=document,f=e.getElementsByTagName("script"),g=f[0].parentNode,h=f[f.length-1],i,j,k=function(){var a=3,b=e.createElement("b"),c=b.getElementsByTagName("i");while(b.innerHTML="<!--[if gt IE "+ ++a+"]><i></i><![endif]-->",c[0]);return a>4?a:0}();try{j=(new Function("return "+h.innerHTML.replace(/^\s+/,"")))()}catch(l){j=null}i=e.createElement("script"),i.async="async",i.src="http://ajax.googleapis.com/ajax/libs/jquery/"+a+"/jquery"+(b?".min":"")+".js",k&&(i.event="onclick",i.id=i.htmlFor=function(){var a,b=0;do a="s"+ +(new Date)+b++;while(e.getElementById(a));return a}()),i.onload=i.onreadystatechange=function(){var a=i.readyState;if(!a||/loaded|complete/.test(a)){if(k)try{i.onclick()}catch(b){}c.call(h,d.jQuery.noConflict(!0),j),i.parentNode&&g.removeChild(i),i=i.onload=i.onreadystatechange=null}},g.insertBefore(i,g.firstChild)})(
// Version of jQuery to load from CDN.
'1.4.4',
// Minified? (always set to true in production)
false,
// Your code (`this` is the script element).
function( $, opts ) {
console.log( $.fn.jquery, opts );
$(this).before( '= replacement html =' ).remove();
});
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>jQuery Bootstrapped Widget Test</title>
</head>
<body>
<div>BEFORE</div>
<script src="bootstrapped_widget.js" type="text/javascript">{ mode: "test" }</script>
<div>AFTER</div>
</body>
</html>
@cowboy
Copy link
Author

cowboy commented Feb 8, 2011

Here's a test fiddle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment