" tag and run with the help of the browser environment; the syntax is "" or "" or "

Home  >  Article  >  Web Front-end  >  Where to put javascript code

Where to put javascript code

青灯夜游
青灯夜游Original
2021-09-02 16:28:193155browse

JavaScript code needs to be placed in the web page using the "<script>" tag and run with the help of the browser environment; the syntax is "<script>JS code</script>" or "".

Where to put javascript code

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

JavaScript programs cannot run independently and can only be executed in the host environment. Under normal circumstances, JavaScript code can be placed in the web page and run with the help of the browser environment.

There are two ways to put JavaScript code into a web page:

Method 1: Use the <script> tag to directly embed the code</script>

Embedding JavaScript scripts in HTML pages requires the use of the <script> tag. Users can directly write JavaScript code in the <script> tag. </script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>第一个JavaScript程序</title>
    <script type="text/javascript">
        document.write("<h1>Hi,JavaScript!</h1>");
    </script>
</head>
<body></body>
</html>

Where to put javascript code

Method 2: Put the JavaScript code into a js file and use the <script> tag to import the js file</script>

JavaScript programs can be placed not only directly in HTML documents, but also in JavaScript files.

JavaScript files are text files with the extension .js and can be edited using any text editor.

JavaScript files cannot 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>

For example: There is a test.js file that contains the following code:

alert("Hi,JavaScript!");

Insert a <script> tag within the <head> tag of an HTML file. 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>

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>

Save the HTML file and preview it in the browser:

Where to put javascript code

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of Where to put javascript code. 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
Previous article:What is javascriptNext article:What is javascript