What Is JavaScript?

JavaScript (which is not actually related to Java) is the most popular scripting language today. Using JavaScript, you can embed programs in Web pages and run those programs. In the next chapter, we'll see how to use those programs to retrieve the contents of XML elements and attributes, and we'll even see how to search XML documents for data.

Internet Explorer provides strong XML support with XML islands, which let you embed XML directly in HTML pages, and by letting you read XML documents directly. In this chapter, we'll learn how to use JavaScript. In Chapter 7, we'll use it to parse XML documents. In Chapter 8, "XML and Data Binding," we'll use JavaScript to load XML documents into database recordsets that have a great deal of support in Internet Explorer, letting you search, order, and display data in many ways.

The programs you write in JavaScript go in the <SCRIPT> HTML element, which itself usually goes into the <HEAD> section of a Web page (however, if you use the script to write text directly to the Web page itself, as we'll do often in this chapter, you should place the <SCRIPT> element in the page's <BODY> element because the <HEAD> section may be parsed before the document's body is available).

Here's an example to get us started. In this case, this JavaScript is writing the text Welcome to JavaScript! directly into a Web page when that Web page is first displayed by the browser:

Listing ch06_01.html
 <HTML>     <HEAD>         <TITLE>             Welcome To JavaScript         </TITLE>     </HEAD>     <BODY>         <SCRIPT LANGUAGE="JavaScript">         <!--             document.writeln("Welcome to JavaScript!")         //-->         </SCRIPT>         <CENTER>             <H1>                 Welcome To JavaScript!             </H1>         </CENTER>     </BODY> </HTML> 

You can see the results of this HTML in Figure 6-1. The JavaScript code wrote the welcoming text you see in the upper-left corner of the page.

Figure 6-1. Using JavaScript in Internet Explorer.

graphics/06fig01.gif

Let's take this example apart to get started. Note that I begin with the <SCRIPT> element and place the JavaScript code into that element. In the <SCRIPT> element, I set the LANGUAGE attribute to "JavaScript" to let the browser know what language the script is in:

 <HTML>      <HEAD>         <TITLE>             Welcome To JavaScript         </TITLE>     </HEAD>     <BODY>  <SCRIPT LANGUAGE="JavaScript">   .   .   .   </SCRIPT>  <CENTER>             <H1>                 Welcome To JavaScript!             </H1>         </CENTER>     </BODY> </HTML> 

I'll enclose the actual JavaScript code inside an HTML comment. What's the purpose of that? It's just general good practice in JavaScript. Unlike an XML browser, if an HTML browser can't understand the contents of an element, it ignores the markup and just displays the text directly. Because some browsers can't understand JavaScript and the <SCRIPT> tag, I place the code in an HTML comment so it won't be displayed in such browsers. JavaScript-enabled browsers ignore the comment markup. (Note that, by convention, you must end the HTML comment with //--> , not just --> , because // is the JavaScript way of creating a comment. I'll go into more depth about this in a few pages.) Otherwise, the browser might try to interpret the --> markup as JavaScript:

 <HTML>      <HEAD>         <TITLE>             Welcome To JavaScript         </TITLE>     </HEAD>     <BODY>         <SCRIPT LANGUAGE="JavaScript">  <!--   .   .   .   //-->  </SCRIPT>         <CENTER>             <H1>                 Welcome To JavaScript!             </H1>         </CENTER>     </BODY> </HTML> 

Handling Browsers That Don't Support JavaScript

There's also a <NOSCRIPT> element that you can use to display messages in browsers that don't support JavaScript (such as "You're missing some amazing JavaScript action!" ). Browsers that don't handle JavaScript won't understand either the <SCRIPT> or <NOSCRIPT> elements, but they will display the text in the <NOSCRIPT> element directly. (Remember, the JavaScript code in the <SCRIPT> element is usually in an HTML comment.) JavaScript-enabled browsers ignore the <NOSCRIPT> element.

At this point, we're ready for the JavaScript code. Here, that code is simply the JavaScript expression document.writeln("Welcome to JavaScript!") ; this expression just writes the text Welcome to JavaScript! to the Web page:

 <HTML>      <HEAD>         <TITLE>             Welcome To JavaScript         </TITLE>     </HEAD>     <BODY>         <SCRIPT LANGUAGE="JavaScript">         <!--  document.writeln("Welcome to JavaScript!")  //-->         </SCRIPT>         <CENTER>             <H1>                 Welcome To JavaScript!             </H1>         </CENTER>     </BODY> </HTML> 

That's our first line of JavaScript: document.writeln("Welcome to JavaScript!") . When the Web page is loaded, this JavaScript is read and run by the browser.

Ending Semicolon in JavaScript Statements

The official standard for JavaScript says that each JavaScript statement should end with a semicolon ( ; ). In other words, this first line of JavaScript should technically be document.writeln("Welcome to JavaScript!"); . However, browsers no longer require the ending semicolon because it's so easy to forget it. It has become usual to omit the semicolon these days, so I have done the same here.

The big two implementations of JavaScript are Netscape's and Microsoft's. These two browser vendors are not exactly the best of friends ; if you assumed that the two implementations of JavaScript have some differences, you'd be right.

Netscape's JavaScript

You can find documentation for Netscape Navigator's JavaScript at http://developer.netscape.com/tech/javascript/index.html. Netscape has also developed a version of JavaScript designed to be used on the server side, not in browsers at all; you can find Netscape's documentation for server-side JavaScript at http://developer.netscape.com/docs/manuals/enterprise/wrijsap/index.htm.

Microsoft's JScript

Microsoft's implementation of JavaScript is both different and more extensive than the Netscape standards. The implementation of JavaScript in Internet Explorer is actually called JScript, not JavaScript, although JScript is very close to JavaScript. You can find the official documentation for JScript at http://msdn.microsoft.com/scripting/default.htm?/scripting/jscript/techinfo/jsdocs.htm. (Note that Microsoft reorganizes its Web sites every 15 minutes or so; by the time you read this, this URL might have changedif so, check http://msdn.microsoft.com/scripting/.)

ECMAScript

There's some animosity between the two main JavaScript implementers, so you might wonder if there isn't some third party that might be able to sort things out. In fact, there is: The European Computer Manufacturers Association (ECMA) in Geneva, Switzerland, has standardized JavaScript. You can find the current standard at www.ecma.ch/ecma1/stand/ecma-262.htm and www.ecma.ch/ecma1/stand/ecma-290.htm. Netscape Navigator's version of JavaScript is ECMA compliant.

You can find plenty of free JavaScript resources out there to learn from. To start, you can find the documentation for Netscape's JavaScript 1.5 online here:

  • The JavaScript 1.5 user 's guide is at http://developer.netscape.com/docs/manuals/js/ core /jsguide15/contents.html.

  • The JavaScript 1.5 reference manual is at http://developer.netscape.com/docs/manuals/js/core/jsref15/contents.html.

  • You can download the JavaScript 1.5 user's guide and reference manual from http://developer.netscape.com/docs/manuals/index.html?content=javascript.html.

You can find the documentation for Microsoft's JScript 5.6 online as well:

  • The JScript 5.6 user's guide and reference manual is available at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsconjscriptfundamentals.asp. Click the links in the navigation bar to find these items.

  • You can also download the JScript 5.6 user's guide and reference manual. The URL for that currently is http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/msdn-files/027/001/733/msdncompositedoc.xml.

The ECMAScript specifications are also online:

  • The ECMAScript Language Specification, 3rd edition, is at www.ecma.ch/ecma1/STAND/ECMA-262.HTM.

  • The ECMAScript Components Specification is at www.ecma.ch/ecma1/STAND/ECMA-290.HTM.

  • The ECMAScript 3rd Edition Compact Profile Specification is at www.ecma.ch/ecma1/STAND/ecma-327.htm.

Many general sites also have good JavaScript resourceshere's a starter list:

  • Netscape's JavaScript resources page is at http://developer.netscape.com/docs/manuals/jsresource.html.

  • You can find some useful JavaScript resources at http://javascript.com/. This is a good JavaScript resource.

  • A good source of JavaScript information is at http://javascript.about.com/.

  • You can find additional JavaScript resources at www.javascriptgate.com/.

And here are a few free JavaScript tutorials on the Web:

  • www.scriptsearch.com/JavaScript/Tips_and_Tutorials/

  • www.scriptsearch.com/JavaScript/Web_Sites/

  • http://javascript.about.com/cs/beginner/index.htm

  • www.w3schools.com/js/default.asp (JavaScript school)

  • www.echoecho.com/javascript.htm

  • www.iboost.com/build/programming/js/tutorial/876.htm

  • www.bitafterbit.com/english/jscript/basic/index.html

  • www.javascriptmall.com/learn/index.htm



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