Skip to content

Instantly share code, notes, and snippets.

@arnorhs
Created March 13, 2011 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnorhs/868180 to your computer and use it in GitHub Desktop.
Save arnorhs/868180 to your computer and use it in GitHub Desktop.
From a blog post on how to create a bookmarklet using jQuery
var i,s,sc=['http://yourhostname.com/path/to/your/script.js'];if(!jQuery){sc.unshift('http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js');}for(i=0;i<sc.length;i++){s=document.createElement('script');s.src=sc[i];document.body.appendChild(s);}void(0);
// define all the variables at the same time:
var i,s,sc = ['http://yourhostname.com/path/to/your/script.js'];
/*
we don't want to add jquery unless it hasn't been defined. If
you're dependent upon a specific version, you should just add
this to the array right away.
*/
if (!jQuery){
sc.unshift('http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js');
}
// loop through the scripts and add them to the document's body.
for (i = 0; i < sc.length; i++) {
s=document.createElement('script');
s.src=sc[i];
document.body.appendChild(s);
}
// this will cause the <a> tag not to try to follow the link on some browsers
void(0);
<p>Drag this link to your bookmarks bar: <a href="javascript:var i,s,sc=['http://yourhostname.com/path/to/your/script.js'];if(!jQuery){sc.unshift('http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js');}for(i=0;i<sc.length;i++){s=document.createElement('script');s.src=sc[i];document.body.appendChild(s);}void(0);">Click me, I'm a bookmarklet</a></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment