Now from beginning of the tutorial series we notice that we write our JavaScript in head section on our html page but what happen in this that we write our JavaScript within our body tag and after our <label> </label>tags ,what happen if we use JavaScript within head lets take look
refreshing our page in browser and we see blank screen ,now we have to check the error so if you are using google chrome in windows use Ctrl+Shift+J and on Mac Cmd+Option+J ,console screen open on your browser which show an error like this
Uncaught TypeError:Cannot set property 'innerHTML' of null at (your code line number) click on that line
Now this is time to understand what going behind the scene of our code
When we write JavaScript code inside the <script> tag which is present in <head>section of our HTML file,our JavaScript execute by our browser rendering engine even before the whole DOM is loaded because it load our whole DOM from top to bottom ,when we place our script in <head>tag our script execute first then the other elements are loaded and our script trying to access the element id before it has render by the DOM in result it throw the null error,As we want that our label shows on page load with values which we assign to our four variables so we write our script after it first DOM load our all labels the execute the script .
Comments
Post a Comment