Search code examples
javascripthtmlquotesbookmarkletbookmarks

Javascript Bookmarklets with Quotes


I have had a problem with making javascript bookmarklets where the code inside contains both "" quotes and '' quotes. Say for example, my code was as follows,

<!DOCTYPE HTML>
<html>
    <head>
        <title>Temporary HTML Doc</title>
    </head>
    <body>
        <a href="javascript:(function(){var a='something';var d='something_else'}());">Link</a>
    </body>
</html>

I would then proceed to open up the HTML file and drag the link to the bookmarks bar. But, since the code has "" and '' quotes, whether or not I write the link as

<a href='javascript:(function(){var a="something";var d='something_else'}());">

or

<a href="javascript:(function(){var a="something";var d='something_else'}());">

, it is still cut off, and the link becomes useless.

So, how can I include code that requires "" and '' quotes to work?
This example isn't my actual code, but it should hold for this problem.

Wikipedia's page on Bookmarklets


Solution

  • To include a " character in an attribute value delimited by " characters, you would normally represent it as an entity: &quot;

    However, since this is a URL, you should encode it for the URL first: %22

    See a live demo.