Using aspnet 3.5.
I have some javascript functions in my aspx file.
Why do the javascript comments get passed to the browser? It makes the download file unnecessarily large.
Using aspnet 3.5.
I have some javascript functions in my aspx file.
Why do the javascript comments get passed to the browser? It makes the download file unnecessarily large.
Javascript is evaluated on the client, so it downloads all the source (including the comments).
Which is why production environments will typically minify/pack Javascript files. This removes unnecessary white-space and comments.
But if the comments are in the HTML file itself (or the HTML document outputted by an ASP.NET page) then the server has to either send the comments to the client or have an extra step to strip them out. The problem is that this process could be relatively expensive since you basically have to parse the HTML output to figure out where the Javascript is and then where the comments are. It's not as simple as a regular expression search and replace (not if you want it to be reliable at any rate).
Unfortunately, the entire block will be sent to the user's browser.
One thing you might want to do is to link to an external script file. Have one with comments for development, and when you're ready to go, strip the comments out and use the minified version.
Because asp.net doesnt do any compression/preprocessing of js files before sending it to the client . You need to use something like YUI Compressor to achieve that .
Because you're not doing anyting to remove them. Look into "minifier"s, such as jsmin from Crockford. These strip out comments and unnecessary whitespace. You integrate them into your build process, so your source file is distinct from what actually gets delivered to the browser.
Why not remove the comments? There are pretty good javascript minifiers available for free
1) compress your javascript before deployment. This is a good thing anyways, because you can get dramatic size reductions even after removing comments
2) if you truly have random bits javascript lying around your aspx file, you could consider using asp server-side comments <%-- --%> instead unlike clientside HTML comments or javascript // comments, these won't get sent over the wire.