Easy to learn JavaScript three: The combination of JavaScript and HTML

Source: Internet
Author: User

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?
    1. <span style="FONT-SIZE:18PX;" ><script type="Text/javascript">
    2. Alert ("Welcome to JavaScript world!!! ");
    3. </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?
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
  5. <title>javascript scripting language </title>
  6. >
  7. </head>
  8. <body>
  9. <p>
  10. JavaScript can be written directly to the HTML output stream:
  11. </P>
  12. <script type="Text/javascript">
  13. document.write ("<H1>this is a heading</H1>");
  14. document.write ("<P>this is a paragraph.   </P> ");
  15. </Script>
  16. <p>
  17. You can only use the <strong>document.write</strong> in the HTML output stream.
  18. If you use it after the document has been loaded (such as in a function), the entire document is overwritten.
  19. </P>
  20. </body>
  21. </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?
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
  5. <title>javascript scripting language </title>
  6. <script type="Text/javascript">
  7. function MyFunction ()
  8. {
  9. document.getElementById ("demo"). Innerhtml="My first JavaScript Function";
  10. }
  11. </Script>
  12. </head>
  13. <body>
  14. <H1>my Web Page</H1>
  15. <p id="Demo">a Paragraph. </P>
  16. <button type="button" onclick="myFunction ()"> click here </button>
  17. </body>
  18. </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?
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
  5. <title>javascript scripting language </title>
  6. </head>
  7. <body>
  8. <H1>my first Web Page</H1>
  9. <p id="Demo">a Paragraph. </P>
  10. <button type="button" onclick="myFunction ()"> click here </button>
  11. <script type="Text/javascript">
  12. function MyFunction ()
  13. {
  14. document.getElementById ("demo"). Innerhtml="My first JavaScript Function";
  15. }
  16. </Script>
  17. </body>
  18. </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?
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; charset=gb2312 " />
  5. <title>javascript scripting language </title>
  6. <script type="Text/javascript" src="/js/myscript.js"></Script >
  7. </head>
  8. <body>
  9. <H1>my Web Page</H1>
  10. <p id="Demo">a Paragraph. </P>
  11. <button type="button" onclick="myFunction ()"> click here </button>
  12. <p><b> Note:</b>myfunction is saved in an external file named "Myscript.js". </P>
  13. </body>
  14. </html>

Myscript.js Code:

[JavaScript]View Plaincopyprint?
    1. function MyFunction ()
    2. {
    3. document.getElementById ("Demo"). Innerhtml="My first JavaScript Function";
    4. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.