Programming with JavaScript

(Tutorial 7 in Textbook)

Including JavaScript Codes can enhance the appearance of your page. You are counting on increasing product sales during the Christmas season and want to remind your viewers of the number of shopping days left before Christmas. Just entering "There are __ days left before Christmas," would require hiring someone to update the page manually each day. It would be much better if this task could be performed automatically by a program running on the Web page itself. You will create such a program in JavaScript, a programming language designed for Web pages. For a more detailed background in JavaScript refer to tutorial seven in your textbook.

The Script Tag

The <SCRIPT> tag is a two-sided tag that identifies the beginning and end of a client-side program. Your program can be placed anywhere within the HTML file, either within the <HEAD> tags or the <BODY> tags. One problem that can be encountered with JavaScript is that older browsers might not support it. To avoid this problem, you can hide the script using comment tags (tags that will not be seen by viewers). When using comment tags within the JavaScript, you must use a set of double slashes(//) at the beginning of a line to tell the browser to ignore the line and not interpret it as a JavaScript command. The syntax for doing this is as follows:

<SCRIPT LANGUAGE="JavaScript">

<!---Hide this script from browsers that don't support JavaScript.

 

//Stop hiding from other browsers -->

</SCRIPT>

Having seen the basic structure of a JavaScript program, you're ready to insert the necessary lines of code into your home page in a text editor. Open this page in Notepad, scroll down the file until you locate the subheading that appears below your business name, and place your cursor after the end heading tag. Press the enter key to place your cursor on a new line. Type in the above script.

With the <SCRIPT> tags and comments in place, your next task is to write a JavaScript program that sends output to the Web page.