1-1 inserting JavaScript in HTML

Source: Internet
Author: User
Tags html comment

1. In the HTML file, use the <script> tag to insert the JavaScript code in the HTML page. <script> label to appear in pairs and write JavaScript code <script></script> between

1 <! DOCTYPE html> 2 ) 9     </script>10 

The JavaScript code that is contained inside the <script> element will be interpreted from top to bottom in turn. Incidentally, it is important not to have a "</script>" string in the <script> embedded JavaScript code that should be encountered in the browser when the string "</script>"

is automatically considered an end and can be resolved using escape characters.

1 <script type= "text/javascript" >2     function sayscript () {3     alert ("<\/script" >    } 5  </script> 

You can also use JavaScript scripts in HTML with the SRC attribute, which is a connection to an external file

1 <script type= "Text/javascript" src= "Xxx.js" ></javascript>

The <script> element with the SRC attribute should not add additional JavaScript code to it, and the additional additions will be omitted automatically.

The src attribute of the <script> element can also be similar to the element, which can point to the full URL in a domain outside the domain of the current HTML page

<script type= "Text/javascript" src= "http://www.xxx.com/xxx.js></sctipt>//does not exist defer drink async attribute, browser will follow < Script> elements appear in the page in order to parse sequentially

Try to put all of the JavaScript references behind the page content in the <body> element, which is advantageous to first render the content in the browser, to avoid a noticeable delay in the browser opening the page.

2. The <script> element defines 6 properties: Async, CharSet, defer, language, SCR, type. Where language is deprecated.

The purpose of the defer property is to indicate that the script does not affect the construction of the page at the time of execution, but the script is deferred until the entire page has been parsed and then run. The Defer property is only suitable for external script files where the HTML5 implementation ignores the defer attributes of the embedded script settings. Again, it is a good choice to put the delay script at the bottom of the page.

In the XHTML document, set the Defer property to

Defer= "Defer"

The asynchronous Script--asyns property, also using an external script file, refers to the immediate download, which specifies that the async attribute is not intended to interfere with other operations on the page. However, it will certainly be performed before the page load event, as far as possible async footsteps do not modify the DOM during the clip.

In the XHTML document, to set the Async property to

async= "Async"


JavaScript code can be included in an HTML comment when it encounters a browser that does not support JavaScript

<script><!--    function Saygood () {   alert ("good!") );     } --></script> 

You can use the NOSCRITP element to degrade the page smoothly when the browser disables JavaScript or when it is not supported.

<script type="text/javascript" defer ="defer" src="xxx2.js" </script> //This script will display a message to the user in case of invalid Users don't see this message when JavaScript is enabled or JavaScript is supported </body>       

                 

1-1 inserting JavaScript in HTML

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.