Search code examples
javascriptjquerygoogle-chromebookmarkletbookmarks

Can a Chrome bookmark contain a lot of javascript?


I'd like to create Chrome bookmarks that perform actions when clicked. The vast majority of them will be manipulating the URL and reloading the page. Can you make Chrome bookmarks that contain large amounts of Javascript? Maybe even jQuery?


Solution

  • The better approach would be using javascript to load an external script and append it to the <head> of the document, this, IMO, makes it easier to work with your script as only the call to load the resources needs to be bookmarklet'd*.

    Example : Load external jQuery script via Bookmarklet

    For example, if you wanted to load jQuery onto the page you're looking at, you could run this bookmarklet from your bookmark bar. (Disclaimer: Best to do a check for jQuery already on the page to avoid conflict. More on that here ).

    javascript(function({var%20external_script=document.createElement('script');20external_script.type='text/javascript';20external_script.src='http://code.jquery.com/jquery-latest.js';document.getElementsByTagName('head')[0].appendChild(20external_script)})();

    *Note that it has been minified and URL encoded. If you use Textmate, there's an option to "Copy Javascript to clipboard as bookmarklet" which does the minifying and encoding automatically.

    Worth pointing out that you can load multiple resources into the DOM with this method, including stylesheets.