js code"; 2. Write the js code into the ".js" file and use "" statement reference."/> js code"; 2. Write the js code into the ".js" file and use "" statement reference.">

Home >Web Front-end >Front-end Q&A >How to add js to html

How to add js to html

青灯夜游
青灯夜游Original
2021-06-21 12:04:417063browse

Methods to add js to html: 1. Embed JavaScript code directly in the page with the syntax "3f1c4e4b6b16bbbd69b2ee476dc4f83ajs code2cacc6d41bbb37262a98f745aa00fbf0"; 2. Write js code into the ".js" file , use the "e48277dfe745cff2295dc91503dd2dbc2cacc6d41bbb37262a98f745aa00fbf0" statement to reference.

How to add js to html

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

There are two main ways to introduce JavaScript into HTML, both of which require the use of the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag. :

Method 1: Embed JavaScript code directly in the page

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JavaScript的引入</title>
</head>
<body>
    <script>
        var a=1;
        alert(a);    
    </script>
</body>
</html>

Method 2: Introduce external files

You can pass The src attribute of the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag introduces an external js file.

Although the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag has many other attributes, these attributes are not commonly used, or they all have default values, and these default values ​​usually do not need to be changed, so the src attribute may be the only one you need to know. properties.

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JavaScript的引入</title>
</head>
<body>
    <!-- 引入本地文件 -->
    <script src="./script.js"></script>
    <!-- 引入外部域的文件 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</body>
</html>

This way the server will send the JavaScript file to the browser with the MIME type application/x-javascript.

Recommended tutorial: "html video tutorial"

The above is the detailed content of How to add js to html. 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