gpt4 book ai didi

javascript - 在页面主体而不是 head 元素中包含脚本有哪些缺点/问题?

转载 作者:数据小太阳 更新时间:2023-10-29 04:34:14 29 4
gpt4 key购买 nike

已经有一个 entry发布关于将 javascript 放入 <head> 的优缺点的帖子元素与关闭正文标签 ( </body>) 之前的对比。

但是我看到有时开发人员将 JavaScript 代码放在 HTML 页面的任意位置。这似乎主要是由于懒惰。在页面的任意位置嵌入 JavaScript 代码有什么缺点?有许多明显的缺点,例如没有缓存、较少重用等。在这方面您还能想到哪些其他缺点?

提前致谢。

最佳答案

读这个:

http://groups.google.com/group/closure-library-discuss/browse_thread/thread/1beecbb5d6afcb41?hl=en&pli=1

The short story is that we don't want to wait for DOMContentReady (or worse the load event) since it leads to bad user experience. The UI is not responsive until all the DOM has been loaded from the network. So the preferred way is to use inline scripts as soon as possible.

<div id="my-widget">&lt;/div>  
<script>
initWidget(document.getElementById('my-widget'));
</script>

Yes, it not as easy to maintain but it leads to a better user experience. By intentionally leaving out DOMContentReady wrappers we have been able to prevent Google Apps to use on this anti pattern.

还有这个:

Using DOMContentReady considered anti-pattern by Google

关于javascript - 在页面主体而不是 head 元素中包含脚本有哪些缺点/问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3093858/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com