JS写在.*.js文件中的好处

news/2024/7/3 1:49:02 标签: javascript, download, security, function, cache, file

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.

 


http://www.niftyadmin.cn/n/944842.html

相关文章

趣味取小数

听说大家最近学得不错&#xff0c;教授就准备考考大家&#xff0c;先做出来有奖哦~ 编写一个函数&#xff0c;取一个小数的第n(-10<n<16)位&#xff0c;当n大于0时&#xff0c;从小数点位置开始向右取小数部分第n位&#xff0c;当n小于0时&#xff0c;从小数点位置开始向…

转 数据库设计中的14个技巧

下述十四个技巧&#xff0c;是许多人在大量的数据库分析与设计实践中&#xff0c;逐步总结出来的。对于这些经验的运用&#xff0c;读者不能生帮硬套&#xff0c;死记硬背&#xff0c;而要消化理解&#xff0c;实事求是&#xff0c;灵活掌握。并逐步做到&#xff1a;在应用中发…

学生成绩的处理

题目描述 期末考试快到了&#xff0c;为了下学期开始的评优&#xff0c;院长给老师下达了任务--做一个统计学生成绩的程序&#xff0c;给他老人家省省事。任务内容是&#xff1a; 编写一个函数void calcscore(int n)&#xff0c;在函数中输入n个人的成绩&#xff0c;计算最高分…

采用gradle构建和发布bboss方法介绍

采用gradle构建和发布bboss版本及从maven中央库下载bboss方法介绍1.概述bboss是国内最早采用gradle来构建和发布版本的开源框架之一&#xff0c;那么gradle是个什么东东&#xff1f;以下公式可以大概表述一下意思&#xff1a;gradleantmaven尤其是通过gretty插件直接可以在ecli…

有序回文数

题目描述 有序回文数是一种很特殊的数&#xff0c;像43211234&#xff0c;321123&#xff0c;现在我把11称为一阶回文数&#xff0c;2112称为二阶回文数&#xff0c;以此类推。 小平刚开始学递归&#xff0c;想用递归的方法输出一个n(<9)阶的回文数。你可以帮助他吗&#xf…

扩展Activiti-5.12轻松实现流程节点间自由跳转和任意驳回/撤回

由于项目需要&#xff0c;最近对开源工作流引擎Activiti-5.12的功能做了一下扩展&#xff0c;实现了以下功能&#xff1a;1.自由流&#xff08;流程节点间自由跳转和任意驳回/撤回&#xff09;2.流程会签任务串并行模式切换一、自由流在已有流程模型的的基础上&#xff0c;每个…

用递归方法求 f(n)

题目描述 C语言习题 用递归方法求 f(n) 1222...n2 ,n的值由主函数输入。 输入 n的值。 输出 f(n) 的值。 样例输入 10 样例输出 385 #include<stdio.h> int main() { int n; scanf("%d",&n); printf("%d\n",fac(n)); return 0; } int f…

解决cxf+bboss发布的webservice缺少wsdl:types和wsdl:message标签的问题

cxfbboss发布webservice服务&#xff08;cxfbboss发布webservice服务方法请参考文档&#xff1a;bbossgroups webservice引擎使用方法&#xff09;&#xff0c;服务发布成功&#xff0c;查看其wsdl文件的时候却缺少<wsdl:types>和<wsdl:message>标签,例如&#xff…