Download Final Study Guide for Introduction to Javascript | IT 1430 and more Study notes Information Technology in PDF only on Docsity! Chapter 9 Part 1: Introduction to JavaScript Dr. Cheryl Aasheim IT 1430 Objectives In this chapter, you will: Learn about the JavaScript programming language Add structure to your JavaScript programs Learn about logic and debugging Java versus JavaScript Java Advanced programming language Created by Sun Microsystems Considerably more difficult to master than JavaScript Java programs are external programs that execute independent of browser JavaScript Runs within a Web page and controls the browser The <script> Element JavaScript programs Run from within a Web page Code is typed directly into Web page code as separate section Called scripts The <script> Element <script> element: tells the Web browser that the scripting engine must interpret the commands it contains. <script type=“text/javascript”> statements </script> JavaScript prompt() Script method used to prompt user for information Syntax var var_name = prompt( message, default ) Example: var name = prompt( 'Please enter your name:', ' ' ); write() and writeln() Methods Document object: Represents browser window’s content To create new text on a Web page while the page is being rendered use the following methods for the Document object: write() method writeln() method write() and writeln() Methods write() and writeln() methods Require a text string as an argument Difference between write() and writeln(): writeln() method adds a line break after the line of text Structuring JavaScript Code Three rules for inserting JavaScript code: Include a <script> element for each code section Place code in the <head> or <body> sections Create a JavaScript source file JavaScript Source File File extension .js Only contains JavaScript statements (no XHTML, no <script> element) Use the src attribute in the <script> element in your XHTML file to access the external .js source file Advantages of using an external .js source file Hiding JavaScript Code Enclose the code within a <script> element in an XHTML comment block Hides embedded code from incompatible browsers JavaScript compatible browsers will ignore XHTML comment tags and execute the JavaScript code normally