Saturday 28 July 2012

Writing HTML in a SCRIPT Element

Writing HTML in a SCRIPT Element

A common error (and the most common source of erroneous bug reports for the WDG HTML Validator) occurs when writing HTML tags within a SCRIPT element:
 
<script type="text/javascript">
<!--
  // This is an error!
  document.write("</P>");
// -->
</script>
 
As mentioned in the HTML 4 Recommendation's note about specifying non-HTML data in element content, end tags are recognized within SCRIPT elements, but other kinds of markup--such as start tags and comments--are not. This is an unintuitive quirk of SGMLfor elements defined to have CDATA content.
Authors should avoid using strings such as "</P>" in their embedded scripts. In JavaScript, authors may use a backslash to prevent the string from being parsed as markup:
 
<script type="text/javascript">
<!--
  document.write("<\/P>");
// -->
</script>

Note that in XHTML, authors must also take care when using start tags within a script element. For details, see the Script and Style elements section of the XHTML 1.0 Recommendation as well as the HTML compatibility guideline for embedded scripts.

No comments:

Post a Comment

Physics basic inventions and inventors

1.Which instrument is used to measure altitudes in aircraft's ? Audiometer Ammeter Altimeter Anemometer Explanation : ...