Excerpt from: http://blog.csdn.net/erlian1992
JavaScript scripts in HTML must be between <script> and </script> tags, and JavaScript scripts can be placed on HTML pages
<body> labels and
One <script> label
To insert a JavaScript script in an HTML page, use the <script> tag. <script> and </script> will tell JavaScript where to start
and end. The lines of code between <script> and </script> contain javascript:
[HTML]View Plaincopyprint?
- <span style="FONT-SIZE:18PX;" ><script type="Text/javascript">
- Alert ("Welcome to JavaScript world!!! ");
- </Script></span>
You don't need to understand the code above. Just understand that the browser interprets and executes JavaScript between <script> and </script>. Those old
Older instances may use type= "Text/javascript" in the <script> tag. It is not necessary to do so now. JavaScript is all modern browsers
and the default scripting language in HTML5. Given that just learning JavaScript language can be used!
JavaScript in the second <body>
In this example, JavaScript writes text to the HTML <body> when the page loads:
Instance code:
[HTML]View Plaincopyprint?
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
- <title>javascript scripting language </title>
- >
- </head>
- <body>
- <p>
- JavaScript can be written directly to the HTML output stream:
- </P>
- <script type="Text/javascript">
- document.write ("<H1>this is a heading</H1>");
- document.write ("<P>this is a paragraph. </P> ");
- </Script>
- <p>
- You can only use the <strong>document.write</strong> in the HTML output stream.
- If you use it after the document has been loaded (such as in a function), the entire document is overwritten.
- </P>
- </body>
- </html>
Let's look at the results of the JavaScript code first, regardless of how it is written and how it works:
Three JavaScript functions and events
The JavaScript statements in the example above will be executed when the page loads. In general, we need to execute code when an event occurs, such as when a user
When you click the button. If we put JavaScript code in a function, we can call it when the event occurs.
JavaScript in the Quad
You can put an unlimited number of scripts in the HTML document. The script can be in the <body> or
In two sections. It is common practice to put functions in the
Does not interfere with the content of the page.
JavaScript functions in the V
In this example, we put a JavaScript function in the
Instance code:
[HTML]View Plaincopyprint?
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
- <title>javascript scripting language </title>
- <script type="Text/javascript">
- function MyFunction ()
- {
- document.getElementById ("demo"). Innerhtml="My first JavaScript Function";
- }
- </Script>
- </head>
- <body>
- <H1>my Web Page</H1>
- <p id="Demo">a Paragraph. </P>
- <button type="button" onclick="myFunction ()"> click here </button>
- </body>
- </html>
The result of the operation is:
The effect after clicking the button is:
Javascrip functions in the six <body>
In this example, we put a JavaScript function in the <body> section of the HTML page. The function is called when the button is clicked:
Instance code:
[HTML]View Plaincopyprint?
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
- <title>javascript scripting language </title>
- </head>
- <body>
- <H1>my first Web Page</H1>
- <p id="Demo">a Paragraph. </P>
- <button type="button" onclick="myFunction ()"> click here </button>
- <script type="Text/javascript">
- function MyFunction ()
- {
- document.getElementById ("demo"). Innerhtml="My first JavaScript Function";
- }
- </Script>
- </body>
- </html>
The result of the operation is the same as the above five results!
Tip: We put JavaScript at the bottom of the page code to ensure that the script is executed after the <p> element is created.
Seven external JavaScript
We can also save the script to an external file. External files typically contain code that is used by multiple Web pages. File extension for external JavaScript files
The exhibition name is. js. To use an external file, set the. js file in the "src" attribute of the <script> tag, and if there is a lot of JavaScript code, I
We advocate the use of external JavaScript, and we typically connect to HTML documents in a separate way.
Instance
HTML code:
[HTML]View Plaincopyprint?
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
- <title>javascript scripting language </title>
- <script type="Text/javascript" src="/js/myscript.js"></Script >
- </head>
- <body>
- <H1>my Web Page</H1>
- <p id="Demo">a Paragraph. </P>
- <button type="button" onclick="myFunction ()"> click here </button>
- <p><b> Note:</b>myfunction is saved in an external file named "Myscript.js". </P>
- </body>
- </html>
Myscript.js Code:
[JavaScript]View Plaincopyprint?
- function MyFunction ()
- {
- document.getElementById ("Demo"). Innerhtml="My first JavaScript Function";
- }
The results of the operation are consistent with the above!
Tip: Referencing a script file in
External scripts cannot contain <script> tags.
Easy to learn JavaScript three: The combination of JavaScript and HTML