Search code examples
javascriptdomjavascript-injection

Why does the page go blank when injecting JavaScript code via the address bar?


So if I want to make a change, for example in this page (stackoverflow.com), using:

javascript:document.getElementById("hlogo").innerHTML = "10";

The page goes blank, only showing the string "10", even though the source code is still there.

Yet if I do this in an HTML document internally via the JavaScript console it doesn't remove the rest of the content visually.


Solution

  • Append a void(0) or any expression returning an undefined value:

    javascript:document.getElementById("hlogo").innerHTML = "10";void(0);
    

    This answer explains this behavior well: https://stackoverflow.com/a/1291950/689788