Javascript Basics Tutorial: How to Insert JavaScript Code

How to write JavaScript code in HTML

Use the <script> tag to insert JavaScript code into HTML web pages. Note that <script> tags should appear in pairs, and write JavaScript code between <script></script>.

<script type="text/javascript"> means that the text between <script></script> is the text type (text). JavaScript is to tell the browser that the text inside belongs to JavaScript language.

<script type="text/javascript">
    document.write("欢迎学习javascript");
</script>

The above code is where we insert javascript code into the html page

Let’s take a look, there is another way to use javascript

Usually we are used to it Call javascript js

Now we have an html file that needs to use js code, but I don’t want to insert the js code into the html, so there will be a lot of code. Usually in this case, we have to create A separate js file, so that if we want to use js code in our html code, we need to introduce this js file:

Next we output a sentence in demo.js, first create this file

Then we introduce js code into the html code

The format is as follows:

<script type="text/javascript" src="demo.js"></script>

src is the address of this file, so we need to write the path correctly. If the path is incorrect, it means that the js code has not been introduced and cannot be used.


Continuing Learning
||
<script type="text/javascript"> document.write("欢迎学习javascript"); </script>
submitReset Code