Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How did HTML introduce JS?


May 29, 2021 Article blog


Table of contents


JavaScript is one of the most popular front-end programming languages for programmers today, and it's powerful enough to add dynamic interaction to web pages, and this article tells you how to import JavaScript into HTML.

1. Use <script > labels

We can use <script> tags to introduce JavaScript code into HTML documents.

The code is as follows:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS - 编程狮(w3cschool.cn)</title>
</head>
<body>
    <script>
        console.log("使用JavaScript")
    </script>
</body>
</html>

When your browser runs this code, it automatically recognizes the <script > in its JavaScript script code... & lt;/script>。 and execute the code in <script> then return the result to HTML, which is then displayed on the browser page.

JavaScript code is introduced on the browser page through <script> tags, where the location of <script> tags can be made possible anywhere in the <head> > tabs and anywhere within <body> tags, but it is recommended that you place the <script> tags in the < body <body> labels. The benefit of this is that javaScript code can be read after the entire page is loaded.

Second, embed external JS files

Embedding an external JS file refers to writing JavaScript to a JS file that saves the file with a .js suffix and then embeds it in HTML through the src syntax of <script> label

The code is as follows:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS - 编程狮(w3cschool.cn)</title>
</head>
<body>
    <script src="w3cschool.js"></script>
</body>
</html>

The w3cshool .js in code is the external file name we write JavaScript, and the src property refers to the javaScript file path that is introduced outside, which is usually a relative path.

Third, JavaScript pseudourl introduction

In most browsers that support JavaScript scripting, we can embed JavaScript script code by calling statements from JavaScript's pseudo-URL address. G eneral format for pseudo-URL addresses: JavaScript:alert ("click the button to jump out"). As we can see here, pseudo-URL address statements typically start with JavaScript and end with our requirements.

The code is as follows:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS - 编程狮(w3cschool.cn)</title>
</head>
<body>
    <button value="点击" onclick="JavaScript:console.log('点击此按钮')"></button>
</body>
</html>

The above is a summary of how the HTML introduced the full contents of JS, like JavaScript students can also enter the following tutorials: JavaScript micro-class, JavaScript basic combat