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 even how to search XML documents for data.

Internet Explorer provides strong XML support with XML islands that let you embed XML directly in HTML pages, by letting you read XML documents directly. In this chapter, we'll learn how to use JavaScript; in the next chapter, 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 record sets 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 read 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:

<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.


Internet Explorer may be the premiere general-purpose XML browser today, but others, such as Netscape, will challenge that in the future. Thus, it's worth bearing in mind that there are some differences in the JavaScript implementation between these two browsers. For example, in the same Web page in Netscape, you must first open the document before writing to it, and then you close it like this:

<HTML>
    <HEAD>
        <TITLE>
            Welcome To JavaScript
        </TITLE>
    </HEAD>

    <BODY>
        <SCRIPT LANGUAGE="JavaScript">
        <!--
            document.open()
            document.writeln("Welcome to JavaScript!")
            document.close()
        //-->
        </SCRIPT>

        <CENTER>
            <H1>
                Welcome To JavaScript!
            </H1>
        </CENTER>
    </BODY>
</HTML>

Let's take this example apart to get started. Notice 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. This is 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 that it won't be displayed in such browsers. JavaScript-enabled browsers ignore the comment markup—although you should note that, by convention, you must end the HTML comment with //-->, not just -->. This is because // is the JavaScript way of creating a comment (I'll go into more depth about this in a few pages). Otherwise, the browser may 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.

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; most of the JavaScript on the Internet these days omits the semicolon, so I will do the same here.

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

Netscape's JavaScript

You can find documentation for Netscape's JavaScript at http://developer.netscape.com/tech/javascript/index.html (bear in mind that such URLs are very subject to change). 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://docs.iplanet.com/docs/manuals/ssjs.html.

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 about every 15 minutes, so by the time you read this, this URL may have changed.)

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 capable of sorting 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 http://www.ecma.ch/ecma1/stand/ecma-262.htm and http://www.ecma.ch/ecma1/stand/ecma-290.htm. (These URLs seem volatile; they changed while the tech review was being done on this book—if they don't work, go to http://www.ecma.ch and search for "ECMAScript.") In fact, some people even call JavaScript ECMAScript these days. Netscape's version of JavaScript is ECMA-compliant.

There are plenty of free JavaScript resources out there to learn from; here's a starter list:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.204.102