Search code examples
javascriptbookmarklet

Is bookmarklet code always written as href attribute value?


As shown in below code,

<div id="content">
            Here is my <a href="javascript:(function(){ 
                                            _my_script=document.createElement('SCRIPT');_my_script.type='text/javascript';
                                            _my_script.src='file:///D:/Access/bookmark/bmlet.js';
                                            document.getElementsByTagName('head')[0].appendChild(_my_script);})();">
                                bookmarklet
                        </a>
        </div>

Is bookmark code always written as a value of hrefattribute of anchor tag?

If yes, Is it not equivalent to writing a JavaScript callback for onClick event?


Solution

  • If you click on the anchor, an anchor with a javascript: URI is mostly similar to having an onclick attribute with the same code.

    But bookmarklets aren't intended to be followed directly. The user is supposed to right-click on it and save the URI to their bookmarks. This only works if the code is in the href attribute.