开发者

Are <script>'s not in <head> ok?

开发者 https://www.devze.com 2023-01-27 10:11 出处:网络
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.

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 开发者_C百科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.


Yes, <script> blocks outside the <head> are fine.

In fact many encourage placing them at the end of the <body>. Placing them just before </body>, defers fetching/loading until after the page content...to speed up the render.

From the W3C HTML4 spec:

The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.


Yes. Its OK. Infact, very often it is necessary. In many cases, you may have some long-loading (read: large download) javascript that is not critical to the page, so you want the rest of your page content to load first, so you put the <script> for that javascript at the bottom of the <body> section.


A script in the body of your document will be executed when it is encountered. If it's a function, then "executing it" simply means defining the function, i.e. adding it to the list of known functions. But if it's not a function, if it's a block of stand-alone statements, they will be executed where encountered.

For example, if you wrote:

<html><head>... whatever ...</head>
<p>Foo
<script>
document.write("bar")
</script>
<p>Plugh
</html>

The browser will display:

Foobar
Plugh

This may or may not be what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消