The easiest way to realize what's going on here is to type javascript:"Hello world"
into the location bar and hitting enter. Note that the page content is replaced, even though you didn't touch the DOM.
That's because the return value of a bookmarklet is handed over to the page content, if it exists. And in this case, (document.body.innerHTML = document.body.innerHTML.replace(/foo/g,"bar"))
evaluates to the new innerHTML
1. Which is just the innerHTML
of the document body, so the stle information is lost when the entire document HTML is replaced.
Far better to do this:
javascript: ( function(){document.body.innerHTML = document.body.innerHTML.replace(/foo/g,"bar") }()
which has the added bonus of making all your var
s private. Or you can just tack on a return false;
to the bookmarklet as @diegog suggested.
1. Not undefined
, in Javascript the equality operator returns the value too, much to the annoyance of those who mistype ==
.