Javascript in the browser

Source: Internet
Author: User
Tags cdata
Using Javascript in a browser can be divided into two methods: inner and outer, which are similar to CSS.

Inline

< Html >
< Head >
< Title > Inline </ Title >
< Script Language = "JavaScript" >
Function Message () {
Alert ("Good day!");
}
</ Script >
</ Head >
< Body > </ Body >
</ Html >

External

< Html >
< Head >
< Title > External </ Title >
< Script Language = "JavaScript" SRC = "../Path/external. js" > </ Script >
</ Head >
< Body > </ Body >
</ Html >


In external. JS, it can contain any JSCodeSuch as class definition and method functions. In contrast, JS files referenced by external methods can be safer, code is easier to manage, and resource overhead can be saved. The reason for saving resource overhead is that when two pages use a JS file at the same time, the JS file will be downloaded only once, avoiding repeated download of the same Code to increase the Page code volume.

Javascript in WebpageSource codeGenerally placed in the

Javascript originally only supports one browser. Therefore, you need to hide JavaScript code in the old browser to avoid errors in Javascript parsing, this is what we often see in the past. Add <! --> <// --> In the current view, this method is no longer necessary. First, it is more appropriate to reference JavaScript code using external methods, second, there are more and more mainstream browsers that support Javascript. Of course, JavaScript code can be correctly identified.

When you browse a webpage, when an image cannot be displayed, you can replace it with text. When the webpage does not support certain JavaScript code, we can also use an alternative method to remind users that their browsers do not support JavaScript or prohibit JavaScript, that is, they use <NoScript> </NoScript> and place it in the body area.

With the emergence of the XHTML (Extensible html) standard, the <SCRIPT> tag has also changed. In the past, I saw some pages use the Ange attribute in the <SCRIPT> tag, and some use the type attribute, I didn't pay much attention at the time. In fact, the latter was defined after the emergence of XHTML. You can set the MIME type to "text/JavaScript" for JavaScript ".

Another change brought about by XHTML is the use of CDATA. Using CDATA can use some special characters in expressions to enhance code readability, do not use "& lt" or "& gt" to indicate that the value is smaller than or greater than the value.

< Script Type = "Text/JavaScript" >
<! [CDATA [
Function Comp (A, B) {
If ( > B)
Alert ( " A is bigger than B " );
Else
Alert ( " A is not bigger than B " );
}
] >
</ Script >

The common writing formats are used in the Code, both of which avoid reading difficulties caused by the use of XML entities. Of course, there is also a problem that the old browser cannot identify CDATA. We need to add annotations to hide the CDATA tag. You can think of the best way to use JavaScript as an external method.

< Script Type = "Text/JavaScript" >
// <! [CDATA [
Function Comp (A, B) {
If ( > B)
Alert ( " A is bigger than B " );
Else
Alert ( " A is not bigger than B " );
}
// ]>
</ Script >

Although some browsers do not fully support XHTML, we should try to write code according to the XHTML standard so that this standard can be more widely supported.

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.