JavaScript Learning Notes (3) How--javascript and HTML are combined

Source: Internet
Author: User

One, JavaScript can be written inside the HTML page, through the <script> tag, such as the bold part of the following code:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <script  type= "Text/javascript">  console.log (  " Hello world! " );  </ script >     </Head>    <Body>    </Body></HTML>

Second, JavaScript can also be written in a separate file, with ". js" as the suffix, the HTML language through the <script> tag's SRC attribute introduced, such as the following code in the bold section:

JS folder to create the new Javascript0.js file:

Console.log ("Hello world!");

The HTML code is as follows:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <Scripttype= "Text/javascript">Console.log ("Hello world!"); </Script>         <script  src= "js/javascript0.js"  type= "Text/javascript"  charset= "Utf-8"></script>     </Head>    <Body>    </Body></HTML>

The Bold section is the introduction of the Js/javascript0.js file, which has the same effect as writing an HTML file, with two benefits:

1. Code Reuse

2. Clear structure

Two points should be noted here:

1. In the JS file does not need to write <script> tags, the tag is to tell the browser engine, the tag inside the JavaScript code, and the JS file has shown that this is the JavaScript code.

2. It is not possible to write JavaScript code in the <script> tag that introduces external JavaScript files, even if writing does not work.

The JavaScript code is executed in the order of the HTML. In general, JavaScript is written at the end of the file, or in a JavaScript method, which is only known after the page is loaded. There are two benefits to doing so:

1. Increase the page load speed, because the JavaScript code executes, the following HTML code will not be rendered, in addition, the JS file download will take time. Improve the user experience.

2. The DOM that prevents JavaScript code from operating has not yet been loaded. Cause an exception.

JavaScript Learning Notes (3) How--javascript and HTML are combined

Related Article

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.