Skip to content Skip to sidebar Skip to footer

Firefox Bookmarklet: Exposing Functions To The Global Namespace

I'm working on a moderately-complex Bookmarklet that works just fine in Chrome, but I can't get it to work in Firefox. When I run my Bookmarklet in Firefox it redirects to a new pa

Solution 1:

It has nothing to do with exposing globals. It has everything to do with the final evaluated value of your bookmarklet.

Any JS code that is evaluated always returns a value which comes from the last line of code or the last block.

For me, in Firefox, both of your examples redirect because both return true. You can test this by pasting the code directly into Firefox console.

Furthermore...

javascript:!function(){...}();

This is weird. I've never seen this pattern.

This is the most common bookmarklet pattern these days:

javascript:(function(){...})();

As long as you don't end that anonymous function with a return, that pattern evaluates to undefined, and no redirect happens.

The older way of achieving the same result was to always use void(0); as the last line of code. That evaluates to undefined also, and if it is the last line, then the entire script evaluates to undefined, and no redirect happens.

Post a Comment for "Firefox Bookmarklet: Exposing Functions To The Global Namespace"