Related Posts

Archive

Remote Bookmark : The bookmarklet

August 1st, 2007

0 : Quick and dirty

javascript:void(function (){ var head = document.getElementsByTagName('head')[0];script = document.createElement(’script’);script.id = ‘makeMark’;script.type = ‘text/javascript’;script.src = ‘http://yoursitehere/makemark.js’;head.appendChild(script)})();

1 : Explanation

javascript:void(

All bookmarklets should have no return value - where would they return to? They are not part of a page. So, this is a void function.

function (){

Keep the variables in their own scope - don’t mess with whatever is already going on.

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

I suppose this line is not really required - document.head should be just as valid.

script = document.createElement('script');
script.id = 'makeMark';
script.type = 'text/javascript';
script.src = 'http://yoursitehere/makemark.js';

Here we are making a reference to your external JavaScript. The SRC, of course, should me modified to fit your site - but it does need to be be an absolute URL.

head.appendChild(script)

Add the code to the head of the current document, as detected above.

})();

Actually run this code.

2 : Conclusion

The interesting thing is that you do not face to call any functions from this bookmarklet - the just-included javascript will run automatically.

The other nice thing about this code is that it can be be put in a standard <a href=""></a> without having to escape strings - this was not necc. true about Versions 1 and 2.

This code was developed and tested with Firefox 2.0.x and seems to run fine on Internet Explorer 7. IE6 has not been tested, but I would expect it to work.

The script is calls is a different matter.

-Sud.

Posted in JavaScript, Remote Bookmark |

Comments are closed.

Previous post: Remote Bookmark : Bookmarklet Architecture

Next Post: Note to self [Ubuntu 7.10 LAMP setup]