我应该在哪里放置我的JavaScript页面或外部文件? [英] Where should I put my JavaScript - page or external file?

查看:95
本文介绍了我应该在哪里放置我的JavaScript页面或外部文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS 2008中,我有一个包含一个母版页的ASP.NET内容页面。我想为此页面添加用于客户端验证等的JavaScript函数。我的问题是:

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. 我应该在单独的 .js 文件中编写这些脚本吗? ,或嵌入在 .aspx 文件中。

  2. 这个选择会影响网站的性能吗?

  3. 是否有编写JavaScript文件的规则?

  1. Should I write these scripts in a separate .js file, or embedded within the .aspx file.
  2. Does this choice affect the performance of the website?
  3. Are there any rules for writing a JavaScript file?


推荐答案

这取决于你的用途。如果它是特定于页面的功能,那么尝试在页面本身中实现它。如果它被许多页面使用,则将其放入js文件中。

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.


它是否会影响
网站的性能? / p>

Does it affect the performance of website?

是的,它可以。如果你有一个包含数千行代码的js文件,那么你必须在js文件中只调用一个函数。如果您引用此文件,则下载整个文件时应该有带宽浪费。在这种情况下,您可以在同一页面内编写该函数。

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.

另一方面,浏览器可以缓存js文件。因此,对于后续请求,如果文件存在于缓存中,则不会再次下载。但是在JavaScript本身写入页面本身的情况下,每次页面加载其所有内容都将被下载。

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.

这篇关于我应该在哪里放置我的JavaScript页面或外部文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆