Where should I put my JavaScript - page or external file?

前端 未结 1 826
时光说笑
时光说笑 2021-01-15 07:04

In VS 2008, I have an ASP.NET content page having one master page. I would like to add JavaScript functions for client side validation etc. for this page. My questions are:

相关标签:
1条回答
  • 2021-01-15 07:48

    It depends on your use. If its a page specific functionality then try and implement it in the page itself. If it is used by many pages then put that inside a js file.

    Does it affect the performance of website?

    Yes, it can. If you have a js file with thousands of lines of code and in a page you have to call only one function inside the js file. If you refer the file for this one there should be a bandwidth wastage in downloading the entire file. For this case you can write the function inside the same page itself.

    On the other side browser can cache a js file. So for subsequent requests if the file is present in the cache it won't be downloaded again. But in the case of JavaScript being written in the page itself, every time the page loads all of its contents will be downloaded.

    0 讨论(0)
提交回复
热议问题