Home > Article > Web Front-end > Where is the javascript code written?
Javascript code can be written in: 1. Use the script tag and write it in the Body part of the Html web page; 2. Use the script tag and write it in the head part of the Html web page; 3. Write it in the suffix " .js" text file, and use the src attribute of the script tag to introduce it into the Html web page.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Where is Javascript written? To sum up, there are three forms:
Internal: in the Body of the Html web page;
Internal: In the head of the Html web page;
External: In the external JS file.
1. Javascript is written in the Body
When the browser loads the Body part of the web page, the Javascript in it is executed. Statement, after execution, the output content will be displayed on the web page.
<head></head> <body> <script type="text/javascript">..Javascript代码..</script> </body> </html>
2. Javascript is written in the head
Sometimes it is not necessary to run Javascript as soon as the HTML is loaded, but the user clicks on an object in the HTML , Javascript needs to be called only when an event is triggered. At this time, such Javascript is usually placed in the head of HTML.
<html> <head> <script type="text/javascript">..Javascript代码..</script> </head> <body> </body> </html>
3. Javascript is written in an external js file
Put the Javascript program into a text file with a .js suffix.
When referencing Javascript in an external file in HTML, you should write a sentence in the Head, where the value of src is the file path of the file where the Javascript is located. The sample code is as follows:
<html> <head> <script src="js/001.js">...</script> </head> <body> </body> </html>
Script in head: a script that needs to be called to be executed or a script that is triggered by an event. When you place the script in the head, you ensure that the script is loaded before any calls.
Script in body: A script that is executed when the page is loaded. It is usually used to generate the content of the page.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of Where is the javascript code written?. For more information, please follow other related articles on the PHP Chinese website!