是否有正确的位置将javascript放在html文件中? [英] Is there a correct place to put javascript within a html file?

查看:68
本文介绍了是否有正确的位置将javascript放在html文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个是因为只有今天我才读到建议,告诉我把它放在< headelement中。

。这是同样的建议我一直都是给b $ b但是没有人解释过为什么。


上周我被告知把它放在底部之前的html< /

body> ;;原因是html不会开始渲染,直到所有的

脚本文件都已下载,如果它们都在头部......正确

现在我'把我所有的javascript放在html文件的末尾似乎

就像明智的做法一样;至少有这样的理由。

解决方案

10月20日凌晨2:25,mark4asp< mark4 .. 。@ gmail.comwrote:


我问这个



你不应该认为我们可以看到或在阅读正文时记住你的

帖子的主题。


因为只有今天我才读到建议告诉我把它放进去

在< headelement中。



合理的建议,因为这是一个允许脚本元素允许的地方,它们也允许在体内。


这是我一直以来的建议

给出但没有人解释过原因。



HTML规范告诉您允许脚本元素的位置:


< URL: http://www.w3.org/TR/html4/interact/。 ..ml#edef-SCRIPT >


>

上周我被告知要把它放在底部之前的html< /

body> ;;原因是html不会开始渲染,直到所有的

脚本文件都已下载,如果它们都在头部......正确

现在我'把我所有的javascript放在html文件的末尾似乎

就像明智的做法一样;至少这是有原因的。



你根本没有提供任何理由。放置脚本的地方

元素取决于它们的预期目的。有两个指导

原则:


1.代码应该在有可能被调用之前加载

并且


2.如果代码需要访问DOM元素,则在它们可能存在之前不应该被称为

,如果是,则不应该出错。 />

你如何实现上述取决于你,但一般的方法是实现第一个是加载包含一般功能的脚本

头部。


有多种方法可以实现第二种 - 插入

函数,用于在DOM准备就绪时运行而不是on Close

在关闭body标签之前的一个脚本元素中。

是其他的,例如使用一般的DOM ready等。功能或等待

发生负载事件。

-

Rob


< blockquote> RobG写道:


10月20日凌晨2:25,mark4asp< mark4 ... @ gmail.comwrote:


>我问这个



您不应该认为我们可以看到或记住您的主题

在阅读身体时发帖。


>因为只有今天我才读到建议,告诉我把它放在< headelement中。



合理的建议,因为这是一个允许脚本元素允许的地方,它们也允许在体内。


>这是我一直给出的相同建议,但没有人解释过原因。



HTML规范告诉您允许脚本元素的位置:


< URL: http://www.w3.org/TR/html4/interact/。 ..ml#edef-SCRIPT >



好​​吧,我读了它,但它没有。它在头部或身体中说。什么

还有吗?


10月20日上午11点37分,自然哲学家< a ... @ b.cwrote :


RobG写道:


10月20日凌晨2:25,mark4asp< mark4。 .. @ gmail.comwrote:


我问这个


在阅读正文时,你不应该认为我们可以看到或记住你的

帖子的主题。


因为只有今天我才读到建议告诉我把它放在< ;>
里面; headelement。


合理的建议,因为这是一个允许脚本元素允许的地方,它们也是允许的身体。


这是我一直以来的建议

给出但是没有有人曾解释过为什么。


HTML规范告诉您允许脚本元素的位置:


< URL:http://www.w3.org/TR/html4/interact/scripts.html#edef-SCRIPT>



好​​吧,我读过它并没有。



是的,确实如此。


它在头部或身体中说。



见?


还有什么?



生命,宇宙和一切。 :-)

-

Rob


I ask this because only today I read advice telling me to put it
within the <headelement. This is the same advice I''ve always been
given but no one ever explained why.

Last week I was told to put it at the bottom of the html just before </
body>; the reason being that html won''t start rendering until all the
script files have downloaded and if they''re all in the head ... Right
now I''m putting all my javascript at the end of the html file seems
like the sensible thing to do; at least there''s a reason for it.

解决方案

On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:

I ask this

You should not assume that we can see or remember the subject of your
post when reading the body.

because only today I read advice telling me to put it
within the <headelement.

Reasonable advice because that is one place that script elements are
allowed, they are also permitted in the body.

This is the same advice I''ve always been
given but no one ever explained why.

The HTML specification tells you where script elements are permitted:

<URL: http://www.w3.org/TR/html4/interact/...ml#edef-SCRIPT >

>
Last week I was told to put it at the bottom of the html just before </
body>; the reason being that html won''t start rendering until all the
script files have downloaded and if they''re all in the head ... Right
now I''m putting all my javascript at the end of the html file seems
like the sensible thing to do; at least there''s a reason for it.

You haven''t provided any reason at all. Where you place script
elements depends on their intended purpose. There are two guiding
principles:

1. Code should be loaded before there is any chance it might be
called, and

2. If code requires access to DOM elements, it should not be called
before they might exist and should not error if it is.

How you achieve the above is up to you, but the general way is to
achieve the first is to load scripts containing general functions in
the head.

There are a variety of ways to achieve the second - inserting
functions intended to be run when the DOM is ready rather than onload
into a script element just before the closing body tag is one. There
are others, such as using a general "DOM ready" function or waiting
for the load event to occur.
--
Rob


RobG wrote:

On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:

>I ask this


You should not assume that we can see or remember the subject of your
post when reading the body.

>because only today I read advice telling me to put it
within the <headelement.


Reasonable advice because that is one place that script elements are
allowed, they are also permitted in the body.

>This is the same advice I''ve always been
given but no one ever explained why.


The HTML specification tells you where script elements are permitted:

<URL: http://www.w3.org/TR/html4/interact/...ml#edef-SCRIPT >

Well I read it and it doesn''t. It says in the head or the body. What
else is there?


On Oct 20, 11:37 am, The Natural Philosopher <a...@b.cwrote:

RobG wrote:

On Oct 20, 2:25 am, mark4asp <mark4...@gmail.comwrote:

I ask this

You should not assume that we can see or remember the subject of your
post when reading the body.

because only today I read advice telling me to put it
within the <headelement.

Reasonable advice because that is one place that script elements are
allowed, they are also permitted in the body.

This is the same advice I''ve always been
given but no one ever explained why.

The HTML specification tells you where script elements are permitted:

<URL:http://www.w3.org/TR/html4/interact/scripts.html#edef-SCRIPT>


Well I read it and it doesn''t.

Yes, it does.

It says in the head or the body.

See?

What else is there?

Life, the universe and everything. :-)
--
Rob


这篇关于是否有正确的位置将javascript放在html文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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