在 HTML 中嵌入 JavaScript 或将其保存在外部有哪些优缺点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34707187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are some advantages and disadvantages for embedding JavaScript in HTML or saving it externally?
提问by Dennis Buntwin
I'm looking for simple bullet point answers please. I've tried looking all over, Googling, other questions here but I can never find both advantages and disadvantages for each method.
我正在寻找简单的要点答案。我试过到处寻找,谷歌搜索,这里的其他问题,但我永远找不到每种方法的优点和缺点。
回答by Kreempuff
This is the answer I got from W3Schoolspertaining to external javascript files
这是我从W3Schools得到的关于外部 javascript 文件的答案
Pros
优点
It allows separation of concerns- which is not a big deal in simple pages but as the script grows larger you can have a monolithic html page. Big files in general are not ideal for maintainability
It allows caching- when the browser loads a script externally (whether it's be from your site or a cdn) it caches the file for future use. That's why cdn's are preferred for commonly used scripts. Makes the browser use a cached script instead of building a new one every time the page loads which makes the page load faster
More readable code- this ties into the first bullet point but nevertheless it is important. The smaller the files we humans are working with the better. It is easier to catch mistakes and much easier to pass of the torch to the next developer working on the project or learning from it.
它允许关注点分离——这在简单页面中不是什么大问题,但是随着脚本变大,您可以拥有一个整体的 html 页面。一般来说,大文件对于可维护性来说并不理想
它允许缓存- 当浏览器从外部加载脚本(无论是来自您的站点还是 CDN)时,它会缓存文件以备将来使用。这就是为什么 cdn 是常用脚本的首选。使浏览器使用缓存脚本,而不是每次页面加载时都构建一个新脚本,从而使页面加载速度更快
更易读的代码- 这与第一个要点有关,但它仍然很重要。我们人类使用的文件越小越好。更容易发现错误,并且更容易将火炬传递给从事该项目或从中学习的下一个开发人员。
Cons
缺点
- The browser has to make an http request to get the code
- 浏览器必须发出 http 请求才能获取代码
There may be other browser specific reasons as well, but I believe the main reason is the separation of code into different components.
可能还有其他特定于浏览器的原因,但我认为主要原因是将代码分成不同的组件。
回答by Eugen Dimboiu
Probably the best advantage of using external javascript files is browser caching - which gives you a good performance boost.
使用外部 javascript 文件的最大优势可能是浏览器缓存 - 这可以为您提供良好的性能提升。
Imagine you have a site that uses MyJsFile.js (a random 50kb javascript file that adds functionality to your websire). You can:
假设您有一个使用 MyJsFile.js 的站点(一个随机的 50kb javascript 文件,可为您的网站添加功能)。你可以:
- embed it in every page, and add the 50kb to every page request (not ideal)
- link it in every page (
<script src="MyJsFile.js"></script>
)
- 将其嵌入到每个页面中,并将 50kb 添加到每个页面请求中(不理想)
- 在每个页面中链接它 (
<script src="MyJsFile.js"></script>
)
The second option is usually prefered because most modern browsers will only get the file once, and serve it from the browser cache instead of downloading it at every request.
通常首选第二个选项,因为大多数现代浏览器只会获取一次文件,并从浏览器缓存中提供它,而不是在每次请求时都下载它。
Check out similar questions:
查看类似问题: