Home  >  Article  >  Web Front-end  >  How to embed js code in html

How to embed js code in html

王林
王林forward
2021-01-12 10:57:503521browse

How to embed js code in html

How to embed javascript code in HTML pages

(Learning video sharing: html video tutorial)

<html> <body> 
<script type="text/javascript"> document.write("Hello World!"); </script> 
</body> </html>

above The code will produce this output in the HTML page:

Hello World!

Explanation of examples:

If you need to insert a piece of JavaScript into the HTML page, we need to use the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag (and use the type attribute to Define scripting language).

In this way, 4ec11beb6c39d0703d1751d203c17053 and 2cacc6d41bbb37262a98f745aa00fbf0 can tell the browser where JavaScript starts and ends.

<html> <body> 
<script type="text/javascript"> ... </script>
 </body> </html>

The document.write field is a standard JavaScript command used to write output to the page.

After entering the document.write command between 4ec11beb6c39d0703d1751d203c17053 and 2cacc6d41bbb37262a98f745aa00fbf0, the browser will execute it as a JavaScript command. This way the browser will write "Hello World!" to the page.

<html> <body> 
<script type="text/javascript"> document.write("Hello World!"); </script>
 </body> </html>

Note: If we do not use the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag, the browser will treat document.write("Hello World!") as plain text, which means that the command itself will be processed Write onto the page.

How to deal with older browsers

Browsers that do not support JavaScript will display the script as the content of the page. To prevent this from happening, we can use HTML comment tags like this:

<html> <body> 
<script type="text/javascript"> <!-- document.write("Hello World!"); //--> </script>
 </body> </html>

The two forward slashes at the end of the comment line are JavaScript comment symbols, which will prevent the JavaScript compiler from compiling this line.

Related recommendations: html tutorial

The above is the detailed content of How to embed js code in html. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete