JS写在.*.js文件中的好处
2009-12-03 11:17
204 查看
JS写在.*.js文件中的好处
先看两个简单的网页文件:
Page1:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript”>
function sayHi() {
alert(“Hi”);
}
</script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
Page2:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript” src=”external.js”></script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
external.js的文件内容如下:
function sayHi() {
alert(“Hi”);
}
两个网页文件的区别:一个是将JS代码直接内嵌到网页文件,一个是以文件的形式引入js代码。页面代码比较简单,但是这两种不同的方式对系统有比一定的影响,一般是不建议使用内嵌的JS代码具体有如下三个原因:
安全性:Anyone can see exactly what the code is doing just by viewing the source of the page.
If a malicious developer examines the code, he might find security holes that could compromise
the site or application. Additionally, copyright and other intellectual property notices can be
included in external files without interrupting the flow of the page.
代码维护:Maintenance—If JavaScript code is sprinkled throughout various pages, code maintenance
becomes a nightmare. It is much easier to have a directory for all JavaScript files so that when a
JavaScript error occurs, there is no question about where the code is located.
缓存:Browsers cache all externally linked JavaScript files according to specific settings,
meaning that if two pages are using the same file, it is only downloaded once. This ultimately
means faster loading times. Including the same code in multiple pages is not only wasteful, but
also increases the page size and thus increases the download time.
先看两个简单的网页文件:
Page1:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript”>
function sayHi() {
alert(“Hi”);
}
</script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
Page2:
<html>
<head>
<title>Title of Page</title>
<script language=”JavaScript” src=”external.js”></script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
external.js的文件内容如下:
function sayHi() {
alert(“Hi”);
}
两个网页文件的区别:一个是将JS代码直接内嵌到网页文件,一个是以文件的形式引入js代码。页面代码比较简单,但是这两种不同的方式对系统有比一定的影响,一般是不建议使用内嵌的JS代码具体有如下三个原因:
安全性:Anyone can see exactly what the code is doing just by viewing the source of the page.
If a malicious developer examines the code, he might find security holes that could compromise
the site or application. Additionally, copyright and other intellectual property notices can be
included in external files without interrupting the flow of the page.
代码维护:Maintenance—If JavaScript code is sprinkled throughout various pages, code maintenance
becomes a nightmare. It is much easier to have a directory for all JavaScript files so that when a
JavaScript error occurs, there is no question about where the code is located.
缓存:Browsers cache all externally linked JavaScript files according to specific settings,
meaning that if two pages are using the same file, it is only downloaded once. This ultimately
means faster loading times. Including the same code in multiple pages is not only wasteful, but
also increases the page size and thus increases the download time.
相关文章推荐
- 把JS与CSS写在同一个文件里的书写方法
- JS与CSS合并写在同一份文件
- 数据请求(写在一个JS文件里,方便调用)
- 多个页面的js写在同一个js文件下
- js 怎么获得写在css文件中的display属性
- 在页面嵌入JS文件的好处
- gulp-rev同时将js和css文件写在一个rev-manifest.json文件里面的方式探讨
- 把JS与CSS写在同一个文件里,还是可以用滴
- jqueryUI小插件2--地域选择--在网页中加div,加id,引入js文件(我写在页面内),调用函数即可
- 把JS与CSS写在同一个文件里的书写方法
- 关于html、css、js是否写在一个文件里
- js使用外部文件包含js的好处
- js和css写在同一个文件里
- 把JS与CSS写在同一个文件里
- springMVC访问静态资源:为什么图片/js/css等文件写在jsp中是404不能获取
- node.js在windows下的学习笔记(9)---文件I/O模块
- ASP.NET使用后台动态加载js和css文件
- nginx下配置的虚拟服务器加载js文件异常
- NodeJS express访问html,css,JS等静态资源文件
- ASP.NET MVC 5 访问在views文件夹中的JS文件。 ASP.NET MVC html与JS分离