1 / 21

Programming with JavaScript (Chapter 10)

Programming with JavaScript (Chapter 10). Various things. Midterm grades: Friday Winter Career Fair Thursday, April 28, 2011 (11 am to 3 pm). MAC Gym, Wade King Student Rec Center http://www.careers.wwu.edu/careerfair_specialevents.shtml

glyn
Télécharger la présentation

Programming with JavaScript (Chapter 10)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with JavaScript(Chapter 10)

  2. Various things • Midterm grades: Friday • Winter Career Fair • Thursday, April 28, 2011 (11 am to 3 pm). • MAC Gym, Wade King Student Rec Center • http://www.careers.wwu.edu/careerfair_specialevents.shtml • bring resumes, dress in business attire, and research participating employers in advance.

  3. Objectives • The script element • Basic JavaScript syntax • Write text to a Web page with JavaScript • JavaScript data types and variables • Create and call a JavaScript function • Access an external JavaScript file • Add comments to JavaScript code • Basic debugging techniques and tools • Example of a Javascript application • Examples of document.write() • Lab4 at http://faculty.wwu.edu/~granier/Spring2011/csci202/labs/lab04/ • LAB 3 ANSWERS csci 202

  4. The Development of JavaScript • Server-side vs Client-side programming • JavaScript is a subset of Java • Differences between Java and JavaScript: • Java is a compiled language • JavaScript is an interpreted language csci 202

  5. Comparing Java and JavaScript csci 202

  6. The Development of JavaScript • Jscript is an IE version of JavaScript • ECMA • European Computer Manufacturers Association • develops scripting standards • The standard is called ECMAScript (aka JavaScript) csci 202

  7. Working with the Script Element • JavaScript code can either be placed • directly in a Web page file or • saved in an external text file • Insert a client-side script in a Web page when using the script element <script type="mime-type"> script commands </script> csci 202

  8. Writing Output to the Web Page • Object: any item • Method: a process by which JS accesses properties of an object • To write text to a Web page, use the following JS code: document.write(“text”); or document.writeln(“text”)’ where text is the content to be written to the page. csci 202

  9. Understanding JavaScript Syntax • JavaScript is case sensitive • Ignores most occurrences of extra white space • Do not break a statement into several lines • The + symbol used in this command combines several text strings into a single text string csci 202

  10. Declaring a JavaScript Variable • Variable: named item designed to store information • JS programs use variables to represent values and text strings • You can declare variables with any of the following JavaScript commands: varvariable; varvariable = value; variable = value; where variable is the name of the variable and value is the initial value of the variable. csci 202

  11. Working with Variables and Data • JavaScript variable types: • Numeric variables • String variables • Boolean variables • Null variables • You must declare a variable before using it csci 202

  12. Working with Variables and Data • Numeric variable: • any number, such as 13, 22.5, etc. • can also be expressed in scientific notation • String variable: • any group of characters, such as “Hello” or “Happy Day!” • Must be enclosed within either double or single quotes • Boolean variable • accepts only true and false values • Null variable • has no value at all csci 202

  13. Working with Variables and Data • JavaScript is a weakly typed language • The + symbol can be used with either numeric values or text strings var total = 5 + 4; var emLink = "cadler" + "@" + "mpl.gov"; csci 202

  14. Creating a JavaScript Function • A function is a collection of commands that performs an action or returns a value • A function name identifies a function • Parameters are values used by the function • The function is executed only when called by another JavaScript command function_name(parameter values) csci 202

  15. Creating a JavaScript Function csci 202

  16. Creating a Function to Return a Value • For a function to return a value, it must include a return statement function function_name(parameters){ JavaScript commands return value; } csci 202

  17. Accessing an External JavaScript File • The code to access an external script file is: <script src="url" type="mime-type"></script> • Place all script elements that reference external files in the document head csci 202

  18. Commenting JavaScript Code • Commenting your code is very important // comment rest of line • Or /* comment block with many lines*/ csci 202

  19. Using Comments to Hide JavaScript Code <script type="text/javascript"> <!--Hide from nonJavaScript browsers JavaScript commands // Stop hiding from older browsers --> </script> csci 202

  20. Debugging Your JavaScript Programs • Debugging is the process of searching code to locate a source of trouble • There are three types of errors: • Load-time errors • Run-time errors • Logical errors csci 202

  21. Debugging Your JavaScript Programs • Microsoft: Microsoft Script Debugger • Firefox: Firefox Error Console • Modular code entails breaking up a program’s different tasks into small, manageable chunks • An alert dialog box can be generated to display a text message with an OK buttonalert(text); csci 202

More Related