More reliable way to get location.href in Javascript Bookmarklet?

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 reliable way to get location.href? Thanks.

+4
source share
1 answer

No. You can try

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

which ensures that you are not using any custom alert function from the global namespace. But you cannot prevent rewriting window.alert so that window.alert = function () { console.log('Haha!'); }; window.alert = function () { console.log('Haha!'); }; .

The location object must be fault tolerant, since the method of the browser’s internal configuration method prohibits any fraud there (which means that the window location attribute is effectively write-protected, like the window.location href attribute)

Edit:
Booklets don't seem to work in Firefox 6+ because of “social engineering bookmarking attacks,” and you can't do anything about it.
fooobar.com/questions/245273 / ...
https://bugzilla.mozilla.org/show_bug.cgi?id=527530

+3
source

Source: https://habr.com/ru/post/1388140/


All Articles