4

This is my current bookmarklet:

javascript:(function(){ alert(location.href); })();

which seems to work fine, but for me, it doesn't work on youtube under chrome for some reason.. is there a more robust way to get the location.href? Thanks.

Timmy
  • 12,468
  • 20
  • 77
  • 107
  • Weird. YouTube gives `about:blank` for some reason (on Chrome 16). – gen_Eric Dec 27 '11 at 14:38
  • 2
    @Rocket Even weirder is that if you write `alert(location.href)` in debugging console - a proper address is shown... – bezmax Dec 27 '11 at 14:46
  • 1
    Ok, it seems I figured this out. For some unknown reason bookmarklet's `Location` object contains the location from last request. And for even less known reason youtube's search box loads `about:blank` with after the main page is loaded (you can see it in developer console network tab). – bezmax Dec 27 '11 at 14:50
  • So how would I get around that? – Timmy Dec 27 '11 at 16:24

1 Answers1

3

No. You may try

(function(window){window.alert(window.location.href)})(this);

which will make sure you're not using some custom alert function from the global namespace. But, there is no way prevent window.alert from being overwritten like so window.alert = function () { console.log('Haha!'); };.

The location object should be fail-proof, since the browser's internal setter method inhibits any shenanigans there (meaning that window's location attribute is effectively write-protected, as is window.location's href attribute)

Edit:
Looks like bookmarklets don't work in Firefox 6+ due to "social engineering bookmarklet attacks", and there's nothing you can do about it.
https://stackoverflow.com/a/6643466/27862
https://bugzilla.mozilla.org/show_bug.cgi?id=527530

Community
  • 1
  • 1
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • 1
    Looks like there's a [Firefox addon](https://addons.mozilla.org/en-US/firefox/addon/inheritprincipal/) that restores the functionality – user123444555621 Dec 27 '11 at 16:49