Straighten out JavaScript (3)-embedding Javascript into webpages

Source: Internet
Author: User
JS Code can be in the head or body area, but there are some differences.
JS is loaded in sequence with other elements on the webpage. After loading, a syntax check is performed first and then executed sequentially;
If JavaScript does not contain the document. Write or document. writeln statement, it is better to write it in the head area. The difference is as follows:


A webpage can contain multiple js code blocks. The code blocks are written as follows:
<SCRIPT type = "text/JavaScript"> </SCRIPT> (currently commonly used)
<SCRIPT type = "application/JavaScript"> </SCRIPT> (the old browser may not support the new rule)

<Script language = "javascript1.5"> </SCRIPT>)

In fact, the current client scripts only include JavaScript and VBScript. It seems that the browser assumes JavaScript first, so it can be abbreviated:
<SCRIPT> </SCRIPT>

However, if the default script of the page is specified in advance, for example:
<Meta http-equiv: = "content-script-type" content = "text/JavaScript">
If the default scripts are inconsistent, it is necessary to specify the type for the script.

Use more external scripts to separate code from the page:
<SCRIPT src = "XXX. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "XXX. js"> </SCRIPT>

JS can also be written directly in the events supported by web pages, or in the href of tag a and the Action attribute of tag form;
In addition, the JS Code in the tag event can omit the "javascript:" prefix. Example:


JS Code is generally written in the function for calling. The return keyword can be omitted during calling;
When calling href and Action attributes, you still need to set "javascript:". Demo:


You can also separate the code, which is especially required when calling external JS, but pay attention to the order and ensure that the object already exists when using the object. For example:


As shown in the preceding example, if you use objects in a webpage in JavaScript code, you must wait for the object to be created, which is inconvenient.
If the keyword defer is added to the <SCRIPT> label, the code is executed at the end, as shown in the following figure: <SCRIPT defer> </SCRIPT>
The so-called final execution is after all the web page code is loaded, but not after all the elements of the web page (such as the image pointed by Src) are loaded;
It will also occur before the onload event at the latest.
In addition, you can use the ID or name attribute to identify elements in a webpage. More methods will be available in the future. Example:


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.