Friday, 26 January 2018

javascript in html head, innerhtml not working?



 







aa








why does the innerHTML not work in the head, but it does work in the body? Excuse the beginners question but last time i used javascript a year ago this was not a problem at all.


Answer



You need to wait for the HTML document to be loaded before it can be manipulated.






Keep in mind that scripts are evaluated in the order they are listed in an HTML document, so your script gets run while the browser is processing the "head" section. Since the browser hasn't yet parsed the "body" section it doesn't know about any element with id "eee".



However, by assigning a function to the "window.onload" event, you can delay the execution of your inner HTML assignment until the window has completely loaded all of the resources and safely manipulate the document.



Note that if your script was in the body section, after the element with id "eee" was listed, then the script would work without the need to wait for the window "load" event.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...