How to use JavaScript and CSS correctly in XHTML documents

How to use JavaScript and CSS correctly in XHTML documents
In more and more websites, the use of XHTML is replacing HTML4 at a fast pace. However, some mainstream browsers do not support XHTML very well. In addition, some web page creators do not understand the differences between XHTML and HTML4 well, which makes the progress of XHTML in the development of the WEB slow.


XHTML is XML not HTML

Currently, one of the main misconceptions about XHTML is that it is just another version of HTML. This misunderstanding is caused by the fact that Microsoft Internet Explorer only supports XHTML in the MIME format text/html and not the recommended application/xhtml+xml format.

When an XHTML page is parsed in the text/html MIME format, it is no different from an HTML page. However, when it is parsed in the text/xml or application/xhtml+xml MIME format, it follows strict XML writing and display rules.

Correctly formatted XHTML is an XML program and must follow strict rules when written:

1. The characters < and & are not allowed to appear in the content of an XHTML document unless they are contained in a CDATA tag (<![CDATA[...]]>)

2. Comment tags (<!--...-->) cannot contain two consecutive hyphens (--)

3. Content contained in comment tags (<!--...-->) will be ignored


Problems with style and script content

The content within the style and script tags will cause some differences when XHTML is parsed as XML format (rather than HTML format).

JavaScript contains characters that cannot exist in XHTML

Some special characters of JavaScript cannot exist outside of the CDATA tag of XHTML.

<script type="text/javascript">
var i = 0;
while(++i < 10){
//...
}
</script>

Note: The sample code above is not well-formed XHTML because it uses the tag " < " which is not allowed in XHTML or XML.


Using comments in style and script content

Authors familiar with HTML usually understand that putting the content of style and script tags in comment tags will hide these contents in the browser, but some browsers cannot understand them.

<style type="text/css">
<!--
body {background-color: blue; color: yellow;}
-->
</style>
<script type="text/javascript">
<!--
var i = 0;
var sum = 0;

for (i = 0; i < 10; ++i)
{
sum += i;
}
alert('sum = ' + sum);
// -->
</script>

The above example shows how to ignore the content within the comment tag in the browser. At the same time, this example also shows the difference between the browser in processing the content of text/xml format and application/xhtml+xml format.

Mozilla 1.1+ / Opera 7
No CSS is applied, no JavaScript is executed

Netscape 7.0x / Mozilla 1.0.x
Do not apply CSS, but execute JavaScript

Internet Explorer 5.5+
The document is not displayed. (See: https://developer.mozilla.org/Ta ... _in_XHTML_Documents )


Style and javascript contain two consecutive dashes (--)

Another problem that arises when using comment tags in JavaScript in XHTML pages is that there will be two consecutive hyphens (--) in the JavaScript:


<script type="text/javascript">
<!--
var i;
var sum = 0;

for (i = 10; i > 0; --i)
{
sum += i;
}
// -->
</script>

Use CDATA instead of comments

Putting the contents of the script tag into a CDATA block can handle the problem of two consecutive dashes in the comment, but this will make some lower-version browsers not support it because they cannot understand XML. Fortunately, we can achieve compatibility by using the comment symbol in JavaScript to comment the CDATA block.

<script type="text/javascript">
//<![CDATA[
var i = 0;

while (++i < 10)
{
// ...
}
//]]>
</script>


Recommended xhtml and html compatibility processing methods

Don't write style and script directly in the XHTML page. A good alternative is to use external files to write CSS and JavaScript, and then import them in XHTML.

This recommendation seems to be a good one. In any case, it will ensure that there will be no problems in the process of converting pages from text/html to application/xhtml+xml, at least in the next few years.

<<:  How to improve Idea startup speed and solve Tomcat log garbled characters

>>:  Detailed explanation of the simple use of MySQL query cache

Recommend

Pitfalls and solutions for upgrading MySQL 5.7.23 in CentOS 7

Preface Recently, I found a pitfall in upgrading ...

Explanation of MySQL performance inspection through show processlist command

The show processlist command is very useful. Some...

Detailed explanation of how to easily switch CSS themes

I recently added a very simple color scheme (them...

Summarize the User-Agent of popular browsers

1. Basic knowledge: Http Header User-Agent User A...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

How to implement https with nginx and openssl

If the server data is not encrypted and authentic...

mysql obtains statistical data within a specified time period

mysql obtains statistical data within a specified...

HTML end tag issue and w3c standard

According to the principles of W3C, each start tag...

Difference between MySQL btree index and hash index

In MySQL, most indexes (such as PRIMARY KEY, UNIQ...

Summary of 10 common HBase operation and maintenance tools

Abstract: HBase comes with many operation and mai...

Summary of things to pay attention to in the footer of a web page

Lots of links You’ve no doubt seen a lot of sites ...

How to write asynchronous tasks in modern JavaScript

Preface In this article, we'll explore the ev...

HTML small tag usage tips

Phrase elements such as <em></em> can ...

js uses the reduce method to make your code more elegant

Preface In actual projects, the most common proce...

Nine advanced methods for deduplicating JS arrays (proven and effective)

Preface The general methods are not listed here, ...