Presentation is loading. Please wait.

Presentation is loading. Please wait.

Project 1: Using JavaScript Essentials for Design JavaScript Level One Michael Brooks.

Similar presentations


Presentation on theme: "Project 1: Using JavaScript Essentials for Design JavaScript Level One Michael Brooks."— Presentation transcript:

1 Project 1: Using JavaScript Essentials for Design JavaScript Level One Michael Brooks

2 Copyright (c) 2004 Prentice-Hall. All rights reserved. 2 Objectives Integrate JavaScript into HTML documents Use inline JavaScript within HTML tags Create simple functions Insert programmer's comments into JavaScript source code

3 Copyright (c) 2004 Prentice-Hall. All rights reserved. 3 Objectives (continued) Hide JavaScript code from incompatible browsers Display alternate content in non- compatible browsers

4 Copyright (c) 2004 Prentice-Hall. All rights reserved. 4 Why Would I Do This? The JavaScript language was created to overcome the limitations of HTML  HTML is a simple scripting language that tells a Web browser how to display a Web page Netscape developed the LiveScript language for use in its Netscape Navigator Web browser  LiveScript’s name was changed to JavaScript with the release of Navigator 2.0 Java and JavaScript are distinct languages and should not be confused

5 Copyright (c) 2004 Prentice-Hall. All rights reserved. 5 Why Would I Do This? (continued) Microsoft developed its own version of JavaScript in Internet Explorer 4.0 called Jscript  The differences between JScript and JavaScript created problems for Web developers  The European Computer Manufacturers Association (ECMA) developed a standardized version of JavaScript, which became known as ECMAScript

6 Copyright (c) 2004 Prentice-Hall. All rights reserved. 6 Why Would I Do This? (continued) JavaScript:  Allows users to interact with Web pages  Allows developers to: create content based on user choices take greater control over the Web browser  Can be used for a number of common applications

7 Copyright (c) 2004 Prentice-Hall. All rights reserved. 7 Why Would I Do This? (continued) JavaScript common applications include:  Creating a rollover, such as a button graphic, that changes when the user rolls the mouse over it  Validating the content of a field in a form  Computing a calculation

8 Copyright (c) 2004 Prentice-Hall. All rights reserved. 8 Why Would I Do This? (continued)  Creating a pop-up window to display an ad  Creating animation using a combination of JavaScript and other technologies, such as Cascading Style Sheets (CSS)

9 Copyright (c) 2004 Prentice-Hall. All rights reserved. 9 Visual Summary JavaScript was designed to enhance HTML, not to replace it JavaScript is often used in HTML documents by simply using the tag  The tag is an HTML tag created to allow the incorporation of other scripting languages within HTML pages

10 Copyright (c) 2004 Prentice-Hall. All rights reserved. 10 Visual Summary (continued) The file name is identified at the top of the window in the main Menu bar The tag allows JavaScript to be inserted into an HTML document The language attribute specifies the scripting language that appears within the HTML Browsers often support a variety of different scripting languages, but JavaScript is the most popular

11 Copyright (c) 2004 Prentice-Hall. All rights reserved. 11 Visual Summary (continued) The tag allows JavaScript to be inserted into an HTML document The language attribute The file name HTML code with added JavaScript

12 Copyright (c) 2004 Prentice-Hall. All rights reserved. 12 Visual Summary (continued) HTML code with added JavaScript displayed in a browser window The user must acknowledge the message, by clicking on the OK button, to continue An alert box is generated by the alert() method

13 Copyright (c) 2004 Prentice-Hall. All rights reserved. 13 Incorporating JavaScript into an HTML JavaScript is a scripting language  A scripting language: is similar to a traditional programming language is usually less powerful and often designed for a specific function  In JavaScript’s case, that specific function is making Web pages more interactive

14 Copyright (c) 2004 Prentice-Hall. All rights reserved. 14 Incorporating JavaScript into an HTML (continued) When you view a Web page in a browser, an interpreter decides how to display the HTML or JavaScript code, and then returns information to the screen  This process is known as parsing

15 Copyright (c) 2004 Prentice-Hall. All rights reserved. 15 Incorporating JavaScript into an HTML (continued) JavaScript is primarily used for client-side scripting  Web browser usually interprets JavaScript code Server-side scripting means the Web server processes the script instead of the user’s computer  Requires more computer resources  Is often necessary for tasks that require secure information, such as processing a credit card transaction

16 Copyright (c) 2004 Prentice-Hall. All rights reserved. 16 Incorporating JavaScript into an HTML (continued) Common languages used for server-side scripting include ASP, Java, Visual Basic, and PHP

17 Copyright (c) 2004 Prentice-Hall. All rights reserved. 17 Incorporating JavaScript into an HTML (continued) JavaScript is simply text that is interpreted by the browser when it is encountered You can use JavaScript in Web sites in several ways:  Embedding code between the HTML and tags function triggerAlert() { alert("function is activated"); } Embedded code

18 Copyright (c) 2004 Prentice-Hall. All rights reserved. 18 Incorporating JavaScript into an HTML (continued)  Using inline code within HTML code Inline code usually means that a single JavaScript command appears inside an HTML tag Typically, this command is associated with an event, such as when someone clicks on a hyperlink Inline code

19 Copyright (c) 2004 Prentice-Hall. All rights reserved. 19 Incorporating JavaScript into an HTML (continued)  Defining code within the section of an HTML document as a function that can be called upon within the body of the HTML document function triggerAlert() { alert("function is activated"); } JavaScript code at the top of the HTML

20 Copyright (c) 2004 Prentice-Hall. All rights reserved. 20 Incorporating JavaScript into an HTML (continued)  Storing JavaScript code in an external file that can be linked to the HTML document This method offers a powerful way to share JavaScript code between multiple documents  Example of storing JavaScript code in an external file that can be linked to the HTML document Example of storing JavaScript code in an external file that can be linked to the HTML document

21 Copyright (c) 2004 Prentice-Hall. All rights reserved. 21 Incorporating JavaScript into an HTML (continued) Using External JavaScript File

22 Copyright (c) 2004 Prentice-Hall. All rights reserved. 22 Incorporating JavaScript into an HTML (continued) Methods  JavaScript commands follow a specific syntax that is stricter than HTML  Commands that perform actions are called methods  A method is always written with parentheses, such as alert()

23 Copyright (c) 2004 Prentice-Hall. All rights reserved. 23 Incorporating JavaScript into an HTML (continued)  Methods may need additional information to perform a specific action  When sending information, such as a text message, quotations are used to mark the beginning and ending of the text

24 Copyright (c) 2004 Prentice-Hall. All rights reserved. 24 Incorporating JavaScript into an HTML (continued) The alert() methodAdditional information sent to the alert() method to perform a specific action  An example of using the JavaScript alert() method

25 Copyright (c) 2004 Prentice-Hall. All rights reserved. 25 Incorporating JavaScript into an HTML (continued) Scripting Versus Programming Languages  Traditional programming languages are converted to “machine code” that can only be read by the intended type of machine (they are platform specific)  Scripting languages are interpreted (parsed) by the software that reads them  The result of this difference is that scripting languages usually run slower, but can be used on different hardware platforms (they are platform independent)  Scripting languages also have built-in security features that keep developers from building harmful or malicious code that could harm a Web surfer’s

26 Copyright (c) 2004 Prentice-Hall. All rights reserved. 26 Using Inline JavaScript Inline JavaScript refers to JavaScript code that is used within an HTML tag Inline JavaScript appears within quotes, the same as HTML tag attributes With inline JavaScript, you do not use the tag Including an Inline JavaScript

27 Copyright (c) 2004 Prentice-Hall. All rights reserved. 27 Using Inline JavaScript (continued) Inline JavaScript is triggered by an event, which is an action the user performs, such as clicking an image or a button  An event handler is a keyword (a word that has a meaning in the language used) that allows the computer to detect an event  JavaScript event handlers can appear as HTML tag attributes or properties  The event handler tells JavaScript when to carry out a command

28 Copyright (c) 2004 Prentice-Hall. All rights reserved. 28 Using Inline JavaScript (continued) In the above example, the alert() command is activated when the mouse is clicked (onClick) The Event Handler

29 Copyright (c) 2004 Prentice-Hall. All rights reserved. 29 Using Inline JavaScript (continued) In the above example, the alert() command is activated when the mouse is clicked (onClick) on the image

30 Copyright (c) 2004 Prentice-Hall. All rights reserved. 30 Using Inline JavaScript (continued) The above example shows an event handler to the tag as inline JavaScript (Internet browser display of this example)Internet browser display of this example The Event Handler

31 Copyright (c) 2004 Prentice-Hall. All rights reserved. 31 Using Inline JavaScript (continued) Once the page is loaded, you will see a welcome message

32 Copyright (c) 2004 Prentice-Hall. All rights reserved. 32 Using Inline JavaScript (continued) Functions  Functions are named, reusable sections of code that can exist in the head or body section of an HTML document, or in an external file  Functions allow developers to reuse code without having to retype it every time it is needed  When you place a function in an external file, it can be shared between all the documents in a Web site  Information can be sent to a function and a function can send information back to other scripts or other functions

33 Copyright (c) 2004 Prentice-Hall. All rights reserved. 33 Using Inline JavaScript (continued)  The keyword function is used to create a reusable, named section of code  Functions are typically triggered by event handlers using inline code that appear as an HTML tag attribute

34 Copyright (c) 2004 Prentice-Hall. All rights reserved. 34 Using Inline JavaScript (continued) In the example above, the function is triggered when the user clicks on the imageimage The function triggerAlert() The function triggerAlert() is triggered by the onclick event handlers

35 Copyright (c) 2004 Prentice-Hall. All rights reserved. 35 Using Inline JavaScript (continued) The function triggerAlert() is triggered when user click on the image

36 Copyright (c) 2004 Prentice-Hall. All rights reserved. 36 Programmer's Comments Programmer’s comments  Programmer’s comments are messages that programmers insert directly into their source code to explain how the code was written  Programmer’s comments are invisible to end users who view the page in the browser, and they are ignored by the JavaScript interpreter

37 Copyright (c) 2004 Prentice-Hall. All rights reserved. 37 Programmer's Comments (continued)  The browser does not display any information contained in a JavaScript comment  This keeps the text created by the second document writeln() command from appearing in the browser window //this page outputs text to the window document.write(“Hello”); /* document.write(“How are you?”); */ Comments not shown in the browser window

38 Copyright (c) 2004 Prentice-Hall. All rights reserved. 38 Hide JavaScript Code from Incompatible Browsers Newer versions of the most commonly used browsers can all interpret comments correctly Older versions of IE, Navigator, and other browsers, however, cannot interpret the comments  Many times, this results in the comments being displayed in the browser window

39 Copyright (c) 2004 Prentice-Hall. All rights reserved. 39 Hide JavaScript Code from Incompatible Browsers (continued) The workaround for this problem is: combining programmer’s comments with HTML comment tags to hide JavaScript code from browsers that cannot interpret the tag or the scripting language contained between the and tags Example

40 Copyright (c) 2004 Prentice-Hall. All rights reserved. 40 Hide JavaScript Code from Incompatible Browsers (continued) comments.html Comments not shown in the browser window

41 Copyright (c) 2004 Prentice-Hall. All rights reserved. 41 Hide JavaScript Code from Incompatible Browsers (continued) The Tag  The tag allows you to create content that will only display in browsers that cannot display the tag  Even if most modern browsers usually support the, it is considered good form to be prepared for the rare circumstance when a particular browser does not support the feature by always including the tag

42 Copyright (c) 2004 Prentice-Hall. All rights reserved. 42 Hide JavaScript Code from Incompatible Browsers (continued)  Example of using the tag The text contained between the pair tags will only display in browsers that cannot display the tag

43 Copyright (c) 2004 Prentice-Hall. All rights reserved. 43 Hide JavaScript Code from Incompatible Browsers (continued)  By default, most browsers are configured to ignore errors generated by scripting languages such as JavaScript  To view errors that are generated by scripting languages, you can turn on the error notifications feature

44 Copyright (c) 2004 Prentice-Hall. All rights reserved. 44 Hide JavaScript Code from Incompatible Browsers (continued) comments.html Your browser doesn’t support JavaScript or JavaScript is disabled. JavaScript is required to view this page correctly. IE browser Error Notification

45 Copyright (c) 2004 Prentice-Hall. All rights reserved. 45 Case Sensitivity Case sensitivity refers to a language’s ability to distinguish between uppercase (capital) and lowercase (small) letters Languages that distinguish between uppercase and lowercase letters are said to be case sensitive A case-sensitive program that expects you to enter all commands in uppercase will not respond correctly if you enter one or more characters in lowercase

46 Copyright (c) 2004 Prentice-Hall. All rights reserved. 46 Case Sensitivity (continued) JavaScript is a case-sensitive language It is different than HTML, which ignores differences between upper-and lowercase letters Users must be aware of the proper spelling of JavaScript commands Errors in case often cause difficult-to-find problems in JavaScript code

47 Copyright (c) 2004 Prentice-Hall. All rights reserved. 47 Case Sensitivity (continued) comments.html Your browser doesn’t support JavaScript or JavaScript is disabled. JavaScript is required to view this page correctly. Since line 7 contains an error: “Document” should start with lower case “d”, depending on which browser you are using and how it is configured, the JavaScript interpreter may display an error message

48 Copyright (c) 2004 Prentice-Hall. All rights reserved. 48 Case Sensitivity (continued) Error shown in the Internet Explorer browser

49 Copyright (c) 2004 Prentice-Hall. All rights reserved. 49 Case Sensitivity (continued) Event handlers used in HTML commands aren’t case sensitive because they appear as HTML attributes Some browsers ignore some case mistakes in JavaScript code and this often creates problems since the developer doesn’t notice the error until the code is tested in another browser


Download ppt "Project 1: Using JavaScript Essentials for Design JavaScript Level One Michael Brooks."

Similar presentations


Ads by Google