In today's web pages, javascript is widely used, which can increase webpage interaction. Javascript simplifies regular html text segments and reduces the download time. Javascript can respond to user operations in a timely manner. Even if the submitted form is checked, you do not need to waste time submitting it to cgl for verification. The features of javascript are endless, as long as you are creative. So what is the use of javascript on Web pages? Let's talk about it in detail.
There are two ways to add Javascript to a webpage:
1. Add HTML documents directly
This is the most common method. Most web pages containing Javascript use this method, for example:
<Script language = "Javascript">
<! --
Document. writeln ("this is Javascript! Use the direct insert method! ");
//-Javascript ends -->
</Script>
In this example, we can see a new tag: <script> ...... </Script>, and <script language = "Javascript"> is used to tell the browser that this is a program written in Javascript, and the corresponding interpreter needs to be mobilized for explanation.
HTML comment tag <! -- And -->: used to remove the Javascript source code that cannot be recognized by the browser, which is very useful for browsers that do not support the Javascript language.
//-Javascript end: the double slash (/) indicates the Javascript comments, that is, the characters starting from // to the end of the line are ignored. As for the document. write () function used in the program, the text in brackets is output to the window. Note that <script> ...... </Script> is not fixed and can be included in
2. Reference Method
If a Javascript source file (with js extension) already exists, you can use this reference method to improve the utilization of program code. The basic format is as follows:
<Script src = url language = "Javascript"> </script>
The Url is the address of the program file. Similarly, such statements can be placed in the HTML document header or any part of the body. If you want to implement the effect of the example in "direct insertion method", you can first create a Javascript source code file "Script. js", whose content is as follows:
Document. writeln ("this is Javascript! Use the direct insert method! ");
On the webpage, you can call the program as follows: <script src = "Script. js" language = "Javascript"> </script>