Embed JavaScript scripts in HTML

Source: Internet
Author: User

There are many ways to embed client JavaScript code into HTML documents:

① Placed between the tag pair <SCRIPT> and </SCRIPT>.

② Place it in an external file specified by the src attribute marked by <SCRIPT>.

③ Place it in the event handle. The event handle is specified by HTML attribute values such as onclick or onmouseover.

④ In a URL, this URL uses a special javascript: protocol.

<SCRIPT> tag:

Client JavaScript code is a part of an HTML file, usually placed between the tag <SCRIPT> and </SCRIPT>.

<script>// your JavaScript code goes here</script>

In XHTML, the content in the <SCRIPT> mark is treated as other content. If the JavaScript code contains <and & characters, these characters are interpreted as XML markup. Therefore, if you want to use XHTML, it is best to put all the JavaScript code into a CDATA section:

<script><![CDATA[//your JavaScript code goes here]]></script>

Scripts in external files:

<SCRIPT> tag supportSRC Properties. The value of this attribute specifies the URL of a file containing JavaScript code. Its usage is as follows:

<script src="../../scripts/util.js"></script>

The extension of a Javascript file is usually. js. It only contains pure JavaScript code, neither the <SCRIPT> tag nor other HTML tags.

The following are some advantages of using the src attribute:

(1)It can remove large JavaScript code blocks from HTML files, which helps to separate content from behavior and simplify HTML files. Using the src attribute is the cornerstone of non-interfering JavaScript programming.

(2)When a function or JavaScript code is shared by several different HTML files, it can be placed in a separate file and then read by the HTML files that require it. This makes the code more maintainable.

(3)If there are more than one page using JavaScript Functions, you can place them in a separate Javascript file so that the browser can cache them so that they can be loaded faster. When JavaScript code is shared by multiple pages, although the browser needs to open a separate network connection to download the Javascript file for the first time, however, the cache saves much more time than this delay.

(4)Because the value of the src attribute can be any URL, javascript programs or web pages from one web server can use the code output by another Web server. Many Internet Advertisements depend on this.

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.