Reputation: 90515
When I type in the address bar in Google Chrome or any other browser:
javascript:alert("hello");
It works, but
javascript:document.body.style.background="Red";
doesn't. Why is that? How to use the address bar to change the background color?
Upvotes: 1
Views: 1821
Reputation: 4624
XMLForDummies that's because of the fact that there is no access to document object from this place.
Upvotes: 0
Reputation: 114407
You need to activate this ability in your Chrome configuration. It's turned off by default.
Upvotes: 0
Reputation: 349082
Add ;void 0
after the bookmarklet. When the code has a non-undefine
value, the page unloads, and the return value is printed.
A common way to write bookmarklets is:
javascript:(function(){ ... code ... })();
undefined
, so the page will not unload.Note that in the latest versions of modern browsers (FF, Chrome), many objects are not available through the javascript:
-URL in the location bar.
Upvotes: 5