简体   繁体   中英

Client-side Javascript can appear where within an HTML document?

Client-side Javascript can appear where within an HTML document?

A. Between the <head> and </head> tags

B. Between the <body> and </body> tags

C. Both of the above

D. None of the above

Here are codes that are placed in the different parts of the HTML. Ever variation will run no matter where the script tag is placed.

1.

<html>
<head>
    <title>Foo</title>
    <script type="text/javascript">
        alert("hello world");
    </script>
</head>
<body>

</body>
</html>

2.

<html>
<head>
    <title>Foo</title>

</head>
<body>
    <script type="text/javascript">
        alert("hello world");
    </script>
</body>
</html>

3.

<html>
<head>
    <title>Foo</title>

</head>
<body>

</body>
    <script type="text/javascript">
        alert("hello world");
    </script>
</html>

If you try out all these 3 variations where the script tags are placed in different parts of the HTML, you'll see that the alert() call will still run; regardless of their position in the HTML document.

So I think the answer to your question is:

C. Both of the above

Since there is no choice that says "Anywhere within the html document"

It is a good idea to place scripts at the bottom of the element. This can improve page load, because HTML display is not blocked by scripts loading.

You should prefer here:

我选择了上面的“C”,因为“脚本”可以放在 和 </body> 以及 <head> 和 </head> 之间的任何位置。在这种情况下,最合乎逻辑的答案是“C”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM