Home >Web Front-end >JS Tutorial >Discuss whether there is any relationship between the storage of JavaScript tag position and its function_javascript skills

Discuss whether there is any relationship between the storage of JavaScript tag position and its function_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:19:511036browse

In a web page, we can place JavaScript code anywhere in the html file, but it is usually placed inside the head or body tag.

Generally speaking, where the 3f1c4e4b6b16bbbd69b2ee476dc4f83a element is placed is closely related to its function. Two situations are discussed here:

1. Place it in the 93f0f5c25f18dab9d176bd4f6de5d30e

The 3f1c4e4b6b16bbbd69b2ee476dc4f83a element is placed in the head so that the browser can read it at the beginning. The 3f1c4e4b6b16bbbd69b2ee476dc4f83a element will be loaded and executed when the entire web page is first parsed, and its priority is second only to 1fd589692e180fb978cda37041cfc5e2 element.

Then parse the rendering downwards.

Application: For example, js for page display initialization must be placed in the head.

2. Place it in the 6c04bd5ca3fcae76e30b72ad730ca86d section

The browser parses the page tags in order, and executes the statement when it reads the JavaScript code.

However, some JS functions are called through events, so where they are placed on the page does not affect the time they take effect. Therefore, after considering the front-end performance issues, you can put the functions that are not executed first The JS code for event calls is placed at the bottom of the body.

Write it and take a look:

<!DOCTYPE HTML>
<html>
<head>
<title>JS代码的位置</title>
<script type="text/javascript">
document.write("I'M HEAD javascript");
</script>
</head>
<body>
<script type="text/javascript">
document.write("I'M body javascript");
</script>
</body>
</html>

From the above, it can be seen that the storage of javascript tag position is closely related to its function. I hope this article will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn