The use of <script> elements in XHTML

Source: Internet
Author: User
Tags cdata

The rules for writing XHTML code are much stricter than writing HTML, such as the following code:

1 <Scripttype= "Text/javascript">2     functionCompare (A, b) {3         if(A<b) {4 Alert ("A is less than B");5         } Else if(A>b) {6 Alert ("A is greater than B");7         } Else{8 Alert ("A is equal to B");9         }Ten     } One </Script>

This code is valid in HTML, but it is not valid in XHTML. The less than sign (<) in the comparison statement a < b here will be treated as starting a new tag resolution in XHTML. However, as a label, there can be no space behind the less than sign, so this will cause a syntax error.

  Solution:

① uses HTML entities (&lt;) in place of all the less-than-sign (<) in the code, although it solves the problem, but it affects the understanding of the Code;

② uses CDATA fragments, as follows:

1 <Scripttype= "Text/javascript"> <! [cdata[ 2     functionCompare (A, b) {3         if(A<b) {4 Alert ("A is less than B");5         } Else if(A>b) {6 Alert ("A is greater than B");7         } Else{8 Alert ("A is equal to B");9         }Ten     } One ]]> </Script>

This method can be used to solve this problem in XHTML-compatible browsers. However, there are many browsers that are incompatible with XHTML and therefore do not support CDATA fragments. Therefore, in order to be compatible with all browsers, you can use the following methods:

1 <Scripttype= "Text/javascript">2 //<! [Cdata[3     functionCompare (A, b) {4         if(A<b) {5 Alert ("A is less than B");6         } Else if(A>b) {7 Alert ("A is greater than B");8         } Else{9 Alert ("A is equal to B");Ten         } One     } A //]]> - </Script>

The use of <script> elements in XHTML

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.