code" or "" ."/> code" or "" .">

Home  >  Article  >  Web Front-end  >  What kind of tag should javascript be placed in?

What kind of tag should javascript be placed in?

青灯夜游
青灯夜游Original
2022-02-21 17:07:203548browse

Javascript should be placed in the script tag. The script tag is used to define scripts. It can contain script statements or introduce script files through the "src" attribute; the syntax is "<script>code</script>" or "

What kind of tag should javascript be placed in?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript code should be placed in the script tag.

<script> tag is used to define client-side scripts, such as JavaScript. The </script>

<script> element can either contain script statements or point to an external script file through the "src" attribute. </script>

Syntax:

<script>js代码</script>
<script src="js文件url"></script>

Example 1:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript">
        document.write("<h1>欢迎来到PHP中文网</h1>");
    </script>
</head>
<body></body>
</html>

What kind of tag should javascript be placed in?

JavaScript programs can not only be placed directly in HTML documents, but also Place it in a JavaScript script file. JavaScript script files are text files with a .js extension that can be edited using any text editor.

Example 2:

Create a new text file and save it as test.js. Note that the extension is .js, which indicates that the text file is a JavaScript type file.

Open the test.js file and write the following JavaScript code in it.

alert("欢迎来到PHP中文网");

Save the JavaScript file. It is recommended to put JavaScript files and web page files in the same directory.

JavaScript files cannot be run independently and need to be imported into the web page and executed through the browser. JavaScript files can be imported using the <script> tag. </script>

Insert a <script> tag within the <head> tag of the html document. Define the src attribute and set the attribute value to a URL string pointing to an external JavaScript file. The code is as follows: </script>

<script type="text/javascript" src="test.js"></script>

Note: When using the <script> tag to include external JavaScript files, the default file type is Javascript. Therefore, regardless of whether the loaded file has a .js extension or not, the browser will parse it as a JavaScript script. </script>

Save the web document and preview it in the browser. The display effect is as shown in the figure.

What kind of tag should javascript be placed in?

Note: The <script> tag that defines the src attribute should no longer contain JavaScript code. If the code is embedded, only the external JavaScript file will be downloaded and executed, and the embedded code will be ignored. </script>

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What kind of tag should javascript be placed in?. For more information, please follow other related articles on the PHP Chinese website!

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