Home >Web Front-end >JS Tutorial >How to use JavaScript code? How to introduce HTML page?

How to use JavaScript code? How to introduce HTML page?

不言
不言Original
2019-01-10 14:59:4122927browse

How to use JavaScript code? How to introduce HTML page? The following article will introduce to you two ways of writing JavaScript code into HTML pages. I hope it will be helpful to you.

How to use JavaScript code? How to introduce HTML page?

#Before learning JavaScript, we should first understand how to use JavaScript code? Let's take a look at the usage of js code.

We use JavaScript code in two ways: One is to embed js code directly in the HTML page, and the other is to link to external JavaScript files.

Let’s look at the first usage first: directly embedding js code in the HTML page

If we embed the JavaScript code directly in the HTML page, we need Using the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag, 3f1c4e4b6b16bbbd69b2ee476dc4f83a and 2cacc6d41bbb37262a98f745aa00fbf0 tells JavaScript where to start and end.

3f1c4e4b6b16bbbd69b2ee476dc4f83a and 2cacc6d41bbb37262a98f745aa00fbf0 The line of code between Script tags can be inserted.

You may see type="text/javascript" used in the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag, but this is no longer necessary. JavaScript is now the default scripting language in all modern browsers as well as in HTML5.

Let’s look at a specific example

<script>
alert("My First JavaScript");
</script>

The above code is to embed js code directly in the HTML page.

Then let’s take a look at the

second usage of js code: by linking to external JavaScript files. Sometimes if there are too many JavaScript codes, we can put the codes separately in JavaScript files and link them together.

We can follow the following steps to link external JavaScript files


1. First, we create a JavaScript file with an extension of .js

2. Then we will write The js code is written into the Js file and the file is saved.

3. Use the script tag to link the JavaScript file to the HTML file

4ea93f0d39ccda8f0fef996443e0ab762cacc6d41bbb37262a98f745aa00fbf0

One thing to note is that in a script tag that has introduced an external js file, you cannot write Js commands in its start tag and end tag.

Let’s look at a specific example

We first create a js file sample.js


Then write the js code

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script>
</body>
</html>

Finally Use script tags to link JavaScript files to HTML files

document.write("<p>This is a paragraph</p>");

The above are the two methods used by js code

The above is the detailed content of How to use JavaScript code? How to introduce HTML page?. 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