Skip to main content

Uncaught TypeError


 
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

Popular posts from this blog

JavaScript In detail Introduction I am trying to write a article series which start from begineer level and slowly slowly move towards professional level,I will try to cover each and every phase of JavaScript programming with possible errors when we code JavaScript and how debug that code also try to cover how JavaScript use with ASP.Net and MVC framework Background Now a days JavaScript become more and more famous language due to it's recent frameworks like Angular,Backbone,React etc. JavaScript frameworks are famous among developers for their efficiency and cost (opensource).  Angular is one of the favorite JavaScript framework for developing single page application(SPA) and it is powered by google.Many developers adapt Angular for their application as well as Angular is highly preferred for hybrid application. But before start work on framework ,you have to take a look on JavaScript,I also tell you one thing that JavaScript itself a very useful la...