Rules

Secret ingredients to quality software

Edit
Info

Do you always put JavaScript in a separate file?

Last updated by Tiago Araujo on 01 Sep 2016 06:40 pm (almost 5 years ago) See History

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
Figure: Bad Code - Using Inline JavaScript

JavaScriptBad
Figure: Bad Code - On PostBack Line numbers are changed for Inline JavaScript

JavaScriptGood
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.

Adam CoganAdam Cogan

We open source. This page is on GitHub