2.1 The HTML Document and JavaScript


Unlike shell and Perl scripts, JavaScript scripts are not stand-alone programs. They are run in the context of an HTML document. When programming on the client side, the first step will be to create an HTML document in your favorite text editor, such as UNIX vi or emacs, or Windows Notepad [1] or WordPad. Since the file you create is an HTML document, its name must include either an .html or .htm extension. JavaScript programs are embedded within the HTML document between the <script> and </script> tags. Please note that throughout the code examples in this book, any whitespace shown at the beginning of the lines is only for easier readability and you should not type it in your actual file. The following example is an HTML file containing JavaScript code:

[1] If you are using Windows Notepad, be sure to turn off word wrap (under the Format menu) to avoid errors in your program.

 
 <html><head><title>Hello</title></head> <body>  <script  language="JavaScript">     <!-- Hide script from old browsers.  document.write("Hello, world!");  // End the hiding here. -->  </script>  <p>So long, world. </body> </html> 

2.1.1 Script Execution

Since a JavaScript program is embedded in an HTML document, you will execute it in your browser window. If using Netscape Navigator or Internet Explorer, follow these instructions:

  1. Go to the File menu and open the file by browsing for the correct one.

    graphics/02inf01.jpg

  2. Click OK to open it. In this example, the file hello.html is displayed in the browser.

    graphics/02inf02.jpg

  3. Or you can type the URL (complete address) in the navigation bar of your browser as shown here.

    graphics/02inf03.jpg



JavaScript by Example
JavaScript by Example (2nd Edition)
ISBN: 0137054890
EAN: 2147483647
Year: 2003
Pages: 150
Authors: Ellie Quigley

Similar book on Amazon

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