Home > Web Front-end > JS Tutorial > body text

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

不言
Release: 2022-04-02 19:37:10
Original
22797 people have browsed it

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 <script> tag, <script> and </script> tells JavaScript where to start and end.

<script> and </script> The line of code between Script tags can be inserted.

You may see type="text/javascript" used in the <script> tag, but this is no longer necessary. JavaScript is now the default scripting language in all modern browsers as well as in HTML5. </p> <p>Let’s look at a specific example</p> <p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false">&lt;script&gt; alert(&quot;My First JavaScript&quot;); &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div><br/>The above code is to embed js code directly in the HTML page. </p><p>Then let’s take a look at the </p><p> second usage of <strong>js code: by linking to external JavaScript files. </strong><strong></strong>Sometimes if there are too many JavaScript codes, we can put the codes separately in JavaScript files and link them together. </p><p>We can follow the following steps to link external JavaScript files</p><p><br/>1. First, we create a JavaScript file with an extension of .js</p><p>2. Then we will write The js code is written into the Js file and the file is saved. </p><p>3. Use the script tag to link the JavaScript file to the HTML file</p><p><script type="text/javascript" src="JavaScript file name and path"></script>

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>
Copy after login

Finally Use script tags to link JavaScript files to HTML files

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!