Do you always put JavaScript in a separate file?

Last updated by TiagoAraujo over 7 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.

We open source. Powered by GitHub