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

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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值