-2

How do I use JavaScript to change the HTML of a webpage? Temporarily, only for the time they are on the webpage I am making a chrome extension to change the 404 that I get at randomsiteisay.com/randomsitepage Pretty much that the unused webpage gets its HTML changed to what I want it to be.

What I have:

var body1 = '\
<div id="header"><h1>Server Error</h1></div>\
<div id="content">\
 <div class="content-container"><fieldset>\
  <h2>Dont go here</h2>\
  <h3>Thats RT, EWW</h3>\
 </fieldset></div>\
</div>\
'

var aNewBodyElement = document.createElement("body");

aNewBodyElement.id = "newBodyElement";
document.body = aNewBodyElement;
document.getElementById("newBodyElement").innerHTML = body1;
user3743254
  • 1
  • 1
  • 3

3 Answers3

0

Just replace the last 4 lines by:

document.querySelector("body").innerHTML= body1
Gaël Barbin
  • 3,769
  • 3
  • 25
  • 52
0
document.getElementById("body").innerHTML = body1;
Undo
  • 25,519
  • 37
  • 106
  • 129
Igor Monteiro
  • 933
  • 1
  • 11
  • 29
  • 2
    Could you please [edit] in an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/questions/148272), because they don't teach the solution. – Scimonster Mar 02 '15 at 20:57
0

To manipulate HTML using javascript just use:

document.getElementById("body"); 
//and then change the background of the body

Or if you want to try jQuery you could do:

$('body').css('background-image', 'url(...)')

might help: Switching a DIV background image with jQuery

Community
  • 1
  • 1
pmat
  • 43
  • 6