Friday, January 26, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...