Why does appending to innerHTML in a bookmarklet overwrite the entire page?

I have this little bookmarklet:

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

Now it's obviously supposed to take the very first div on the page, and add Chuck Norris into it.

Instead, when pasted on the address bar, Chuck Norris overwrites the page.

Why is this so?

Note: this doesn't occur on Safari...

Answers:

Answer

You are not cancelling the action. add void 0; to then end.

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

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

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.