javascript code in aspx file
We have a .aspx file which has about 400 lines of javascript code. Is it a good idea to have such huge code in its own file? What is the performance difference in having huge javascript code in aspx as against the .js file?
开发者_Python百科Please advise
If it is in its own file, then clients will only have to download it once, rather than parse it out of the page every time.
If you move that logic into a .js file (which I recommend), the browser can cache the JS file, making your page load faster.
Also, the logic (functions) in that JS file could then be used from other pages.
You'll want it in a separate file for two reasons:
- If you need to reuse any of that code, it would be better to have it in a
.js
file to keep your code DRY. - If it is not updated as often as the page is updated (i.e., on every postback) then it is better to have it in a
.js
file for caching.
Not sure, but it seems like it would be easier to compress the JS code when it's in a separate file.
for including js file in aspx, you generally use a string builder, If it so big you need a lot of codes in aspx page. Dont waste client side scripts in server side space. Just use it as an external js file.
精彩评论