script Using Script Programming

<script> Using Script Programming

You use the <script> element to embed a script, such as those written in JavaScript in an XHTML document. You usually place this element in a document's <head> section, except when the code writes directly to the document's bodyin that case, you should place it in the document's body. This element is supported in XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, and XHTML 1.1. Here are the attributes of this element:

  • charset Gives the character encoding of the script contents. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, XHTML 1.1.)

  • defer Tells the browser that the script is not going to generate any document content. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, XHTML 1.1.)

  • language Specifies the scripting language. This attribute is required if the src attribute is not set; it is optional otherwise . (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset.)

  • src Holds a URI for the script code. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset.)

  • type Required. Holds the Multipurpose Internet Mail Extension (MIME) type of the scripting code. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset.)

  • xml:space Set to preserve to preserve spacing. (XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, XHTML 1.1.)

This element does not support any XHTML events.

You use the <script> element to embed a script in a document. In XHTML, the type attribute, which you set to "text/javascript" in JavaScript, is required. In the following listing, note that, normally, <script> goes in a document's <head> element; however, because this script writes a messge, Welcome to XHTML scripting! , to the document's body, it's supposed to be in the <body> element. (Note also that the document. open () and document.close() calls are not needed in Internet Explorer.)

Listing ch17_12.html
 <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">     <head>         <title>             Welcome to XHTML scripting         </title>     </head>     <body>  <script type = "text/javascript" language="javascript">   document.open()   document.writeln("Welcome to XHTML scripting!")   document.close()   </script>  </body> </html> 

You can see the results of this XHTML in Figure 17-6.

Figure 17-6. Running JavaScript in XHTML.

graphics/17fig06.gif

In HTML programming, you used to enclose the actual JavaScript in an HTML comment like this (note that I end the comment with a JavaScript comment marker, // ; if I didn't, some HTML browsers would try to read --> as script):

 <?xml version="1.0"?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">     <head>         <title>             Welcome To JavaScript         </title>     </head>     <body>         <script type = "text/javascript" language="javascript">  <!--   document.open()   document.writeln("Welcome to XHTML scripting!")   document.close()   //-->  </script>     </body> </html> 

You used to do that because if a browser doesn't understand the <script> tag, it'll display what's inside the <script> elementthe JavaScript codeas text. If you enclose that text in what looks like an HTML comment to the browser, it won't display the code. However, that's no good in XML because XML parsers are allowed to remove what they consider to be comments from the document entirely and not pass them on to the browser.

While we're on the topic, it's worth noting that if your script contains sensitive characters such as < and & , they may be treated as markup by an XHTML browser. To avoid that, the W3C helpfully suggests that you enclose the script in a CDATA section:

 <?xml version="1.0"?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">     <head>         <title>             Welcome to XHTML scripting         </title>     </head>     <body>         <script type = "text/javascript" language="javascript">  <![CDATA[   document.open()   document.writeln("Welcome to XHTML scripting!")   document.close()   ]]>  </script>     </body> </html> 

Unfortunately, no major HTML browser today has any idea what to do with CDATA sections in documents that the browsers consider to be HTML. Your only real alternative here is to store the script in an external file so that it won't be parsed and assign the src attribute the URI of that file. Here's how that looksin this case, I'm storing the JavaScript code in a file named ch17_14.js:

Listing ch17_13.html
 <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">     <head>         <title>             Welcome to XHTML scripting         </title>     </head>     <body>  <script type = "text/javascript" language="javascript"   src="ch17_14.js">   </script>  </body> </html> 

Here, ch17_14.js, stored in the same directory as the Web page itself, contains this code:

Listing ch17_14.js
 document.open() document.writeln("Welcome to XHTML scripting!") document.close() 

This XHTML gives the same results as the earlier examples. What's important to remember here is that you should use external scripts if your script uses the characters < , or & , or ]]> , or -- because an XHTML browser may interpret those characters as markup.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net