xhtml| Standard
Recently, with the advent of XHTML (extensible HTML) standards,,<script/> tags have undergone some changes. The label no longer uses the language attribute, and the type attribute declares either inline code or the MIME type of the external file to be added, and JavaScript mime is "Text/javascript". For example:
<title>title of Page</title>
<script type= "Text/javascript" >
var i = 0;
</script>
<script type= "Text/javascript" src= ". /scripts/external.js "></script>
<body>
<!--body goes-->
</body>
Even though many browsers do not fully support XHTML, most developers now use the type feature instead of language features to provide better XHTML support. Omitting the language feature does not cause any problems because, as previously mentioned, all browsers default to <script/> This property value is JavaScript.
The second change in XHTML is the use of CDATA segments. CDATA Segments in XML are used to declare text that should not be parsed into labels (as is the case with XHTML), so that special characters such as less than (<), greater than (>), and (&) and double quotes ("") are used without having to use their character entities. Consider the following code:
<script type= "Text/javascript" >
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
</SCRIPT>
This function is fairly simple, it compares numbers a and B, and then displays a message stating their relationship. However, in XHTML, this code is invalid because it uses three special symbols, which are less than, greater than, and double quotes. To fix this problem, you must replace them with the three-character XML entity <, >, and ":
<script type= "Text/javascript" >
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
131
JavaScript in the Browser
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
</SCRIPT>
There are two problems with this piece of code. First, developers are not accustomed to writing code with XML entities. This makes the code difficult to read. Second, in JavaScript, this code will actually be considered a syntax error because the interpreter does not know the meaning of the XML entity. Using CDATA segments, you can write JavaScript code in a regular form (that is, readable syntax). The way to formally add a CDATA section is as follows:
<script type= "Text/javascript" >
<! [cdata[
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
]]>
</SCRIPT>
While this is a formal approach, remember that most browsers do not fully support XHTML, which leads to the main problem that this is a syntax error in JavaScript because most browsers do not recognize CDATA segments.
The solution that is currently in use mimics the method of hiding the old browser code. Using a single-line javascript annotation, you can embed a CDATA segment without affecting code syntax:
<script type= "Text/javascript" >
<! [cdata[
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
]]>
</SCRIPT>