Created
March 13, 2011 15:43
-
-
Save arnorhs/868180 to your computer and use it in GitHub Desktop.
From a blog post on how to create a bookmarklet using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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