Why is adding to innerHTML in a bookmark rewrite the whole page?

I have this little bookmarklet:

javascript:document.getElementsByTagName("div")[0].innerHTML+="Chuck Norris";

Now he obviously needs to take the first one divon the page and add Chuck Norris to it.

Instead, when you paste into the address bar, Chuck Norris rewrites the page.

Why is this so?

Note: this does not happen in Safari ...

+5
source share
2 answers

You do not cancel the action. add void 0; then over.

javascript:document.getElementsByTagName("div")[0].innerHTML+="Chuck Norris";void 0;
+5
source

I'm not sure what the exact problem is, but I got it working by creating a textNode and adding it:

javascript:var d=document.getElementsByTagName("div")[0];var n=document.createTextNode("Chuck Norris");d.appendChild(n);
-1
source

All Articles