Home
Do you always put JavaScript in a separate file?
  v1.0 Posted at 2/09/2016 4:40 AM by Tiago Araujo
ASP.NET injects many lines during page rendering, so if you are using inline JavaScript, the line numbers will change during client side JavaScript debugging in VS.NET, FireBug or IE8 developer Tools.
​​​
JavaScriptBad1.jpg
Figure: Bad Code - Using Inline JavaScript
JavaScriptBad.jpg
Figure: Bad Code - On PostBack Line numbers are changed for Inline JavaScript
JavaScriptGood.jpg
Figure: Good Code - Using JavaScript on Separate file ​

So you should always put JavaScript in a separate file.  Then the line numbers will stay consistent during debugging. 
Keeping JavaScript in a separate file is also good for production as it improves performance due to browser caching. 

Note: During development, remember to hit CTRL-F5 to force the browser to re-fetch the files from the server or you may be debugging old version of the JavaScript file.

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.