JavaScript Java vs JavaScript Java More powerful language Requires a JDK Compiled language (requires compiling software and JVM) Source code is hidden from the user JavaScript Simpler to author Interpreted language (requires only code and browser) Source code is visible to the user JavaScript should not be thought of as Java Lite, it has separate roots than Java JavaScript and HTML Script elements can be placed directly into an HTML file, or the HTML can link to an external file. Script elements are placed into the HTML document in the following format: <script type=”mime-type”> script commands </script> For example: <script type=”text/javascript”> javascript commands </script> Unlike the style element, <script> is not constrained to the head of the document, and can therefore be placed in the body section. JavaScript and XHTML JavaScript uses XHTML reserved characters which cause parsing errors. Possible solutions: 1- You can place your data in a CDATA section: <script type=”javascript”> <! [CDATA [ javascript ]]> </script> 2- Link to an external script file. JavaScript Basics: JavaScript is composed of statements, or commands. All statements MUST end with a semicolon. JavaScript is case sensitive. Variables: A value stored in memory Javascript is loosely typed Basic variables include: numeric, string, and Boolean Variable naming rules: 1234- name must begin with letter or underscore. name must be made up of letters, numbers, and underscores. the name cannot be a reserve word. the name cannot contain spaces or punctuation. Variables are declared in the following method: var variablename; For example: var year; No assigned value var year = 2007; numeric type var year = “2007”; string type var year = true; Boolean type Expressions: Assigning a value to a variable, such as: average = totalValue/count; count = 0; Operators indicate that an expression in with the data must to something, such as an arithmetic operator telling the data to do addition, or string concatenation. Operators: Symbol Description = + * / ++ -- assigns a value to addition (numeric or string concatenation) subtraction multiplication modulus increment operator decrement operator Conditionals: Boolean, conditionals can only be true or false If, If…Else, and While statements are all conditional statements JavaScript conditionals are given the following format: conditional { Actions if the conditional is met } Example: <script type="text/javascript"> var todayDate = new Date(); var numHours = todayDate.getHours(); document.write(todayDate); if(numHours >= 12) { document.write("Good Afternoon"); } </script> Objects: JavaScript makes use of Objects but is not a complete OOP language. Objects are defined by their properties, and what the object can do (methods). JavaScript supports a number of built-in objects including: Date, Document, Array, window, form, text box, and check boxes. Events: JavaScript supports event based interaction An Event is an action that occurs based on an interaction Events require an event handler which defines the type of event that will trigger an action. Examples of handlers: onClick onKeyPress onMouseDown onMouseOver Handing information to HTML Data can be “printed” in the HTML file using the method document.write(); For example: document.write(“Hello World”); where document refers to the document object (or HTML file executing the script) .write() is a method within the document class that passes the value within the brackets to the HTML file. Note that information passed to the HTML document will be formatted by the HTML and the styles that are applying to the element that the data is printed to.