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.