javascript bookmarklet redirecting?

in both firefox and IE the bookmarklet



successfully changes the page background but then immediately redirects to a page reading "url('')". the code works fine when actually embedded in an html page. obviously, i want to remain on the current page. the two line breaks are there because this site is stupid.

1 Answer

  • om
    Lv 6
    7 years ago
    Favorite Answer

    The browser will replace the page content with the value produced by the bookmarklet, *unless* that returned value is the special JavaScript value 'undefined'.

    Your bookmarklet doesn't explicitly produce a value, but its implicit value is the value of the last statement in the bookmarklet. Here there's only one statement, and it's an assignment of a string whose value is "url( '' )". Therefore that string replaces the page content.

    To prevent this from happening you need to arrange for your bookmarklet to produce the magic 'undefined' value. There are various ways to do this, but the most common one is to end your bookmarklet with the statement 'void(0);'. void() is a JavaScript function that is defined to always yield 'undefined'. So, change your bookmarklet to this:

      javascript: = "url( '' )"; void(0);

    and that should make it work the way you want.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.