< script>不在< head>中好? [英] Are <script>'s not in <head> ok?

查看:78
本文介绍了< script>不在< head>中好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经有一段时间在思考这个奇怪的问题,但我想我会问你的集体专业知识来解答这个问题。



<要详细说明标题,说我有这个:

 
alert(Some JS outside);

在我的HTML的< head>< / head> 标签之外文件。我的问题是这样做是否可以,以及它的用途是多少。



我的直觉告诉我没关系 - 我认为浏览器会浏览所有的HTML对于< script> 标记并在他们看到它时对其进行解释,所以它应该可以,但我并不是那么棒,浏览器正常工作。



我正在寻找一个明确的(或尽可能接近最终的)答案 - 是否可以,或者不行?



编辑:为了节省我发布这一堆的时间,我会在这里说一次。非常感谢您的所有输入人员。向大家投票!我将不得不重新训练自己将JS放在页面的底部 - 现在我想到了这一点,非常明显,页面底部的脚本比顶部好得多。感谢您的帮助。

解决方案

您的脚本标签的最佳位置位于您的正文标签之前。
$ b

 < html> 
< head>
< title>示例< / title>
< / head>
< body>
您的内容
< script type =text / javascriptsrc =yourScriptHere.js>< / script>
< script type =text / javascript> //内联脚本等< / script>
< / body>
< / html>

也就是说,他们可以在其他地方没有问题,但最终你想要他们的原因是您希望确保页面在执行前已经加载,并且您也不希望拖延客户端下载进度,导致它们在大型脚本中等待。


I have had this question niggling at the curious bit of my mind for a while now and I thought I'd ask your collective expertise for an answer to this question.

To elaborate on the title, say I have this:

alert("Some JS outside ");

Outside the <head></head> tags of my HTML file. My question is whether it's ok to do this or not, and how much it is used like this.

My instincts tell me it's ok - I reckon browsers look through all HTML for <script> tags and interpret it when they see it, so it should be ok, but I'm not all that great with how browsers work.

I'm looking for a definitive (or as close as possible to definitive) answer here - is it fine to do, or not?

EDIT: To save me posting this a bunch of times, I'll say it once here. Thanks very much for all your input people. Up votes to you all! I will have to re-train myself to put JS at the bottom of pages - now that I think about it it's blindingly obvious that scripts at the bottom of the page is way better than the top. Thanks for your help everyone.

解决方案

Best place for your script tags is before your closing body tag.

<html>
      <head>
      <title>Example</title>
      </head>
      <body>
            Your Content
      <script type="text/javascript" src="yourScriptHere.js"></script>
      <script type="text/javascript">//Inline scripts etc.</script>
      </body>
</html>

That said they can be other places without a problem however the reason you want them at the end is that you want to ensure the page has loaded before execution and you also do not want to stall client download progress making them wait on large scripts.

这篇关于&lt; script&gt;不在&lt; head&gt;中好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆