Javascript

you can 'mix' html and javascript on same page <head> (javascript) </head> <body> (javascript) </body> Another javascript example: <html><head> <title> A JavaScript Example </title> <script> var name=window.prompt("Hello, what is your name?", " "); document.write("Hello, " + name + "! I hope you like JavaScript!"); </script> </head> For older browsers, you may need to prepare for the inability to process javascript <script language "_______" <!-- | | (script stuff here) | | //--> </script> This hides the script from the browser if it is not able to process that script language In JavaScript: x = 0 =>this is a global variable, with a scope of the entire document var x = 0; { (function) } => this has local scope Control structures in JavaScript, (a lot like C++) if(condition) { statements for true } else { statements for false } for(initExpr; condition; incrExpr) { statements to execute } EXAMPLE; for(x=1; x<=10; x++) { y = x * 25; document.write("x=" + x + "<BR>" + "y=" + y + "<BR>"); } 'while', 'continue' and 'break' are also like C++ <body bgcolor=white text=black link=red vlink=red alink=blue> <h1 align = "center"><font color="red"> CIS525 - Lecture#7 - September 25, 2000 </font></h1> Objects in Javascript - created can be similar to objects in C++, with some differences this.firstname = firstname; => accessing and assigning An Object Example: <html><head> <title> An Object Example </title> <script language "JavaScript"> function person(firstname, lastname, age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } person1 = new person("logan", "blankenschplatz", "87"); Another way of creating/assigning objects: var test = new Object(); test.field1 = "value1"; Caution: there is no 'spellchecking' in JavaScript!! Any misspelling may result in an unwanted new field In JavaScript 1.2, you can assign values thusly: Object2 = (x:3, y:4, z:5) This is equivalent to: var Object2 = new Object(); Object2.x = 3; Object2.y = 4; Object2.z = 5; In JavaScript, "function" is a reserved word: function square(x) { return(x*x); } Recursion: function factorial(n) { if(n<=1) return(1); else return(n*factorial(n-1)); } For dynamic html: function printheading(message) { document.writeln("<H1>" + message + "</H1>"); } In JavaScript, the attribute of an object can be a function return An example: this.printStats = printStats; function printStats() { document.write(this.firstname + " "); document.write("age=" + this.age + " "); } JavaScript has a parser utility: parseFloat, (or parseString, parseInt, etc), an example: parseFloat("137") =>returns 137 parseFloat("137abc") =>returns 137 parseFloat("abc137") =>returns 0, doesn't see a number, and breaks parseFloat("1abc37") =>returns 1, sees first number, then breaks Arrays in JavaScript: myArray = InitArray(10); myArray[1] = "South Carolina"; myArray[2] = "Oregon"; =>assigning values to indexes OR: var squares = nw Array(5); for(var i = 0; i < squares.length; i++) squares[i] = i*i; } OR: var squares = new Array(0, 1, 4, 9, 16); Different methods of assigning values to indexes: var ArrayObj = new Object(); ArrayObj[0] = "Index zero"; ArrayObj[10] = "Index ten"; =>nothing is placed in indices 1-9 ArrayObj.field1 = "field1"; =>index in this case is actually text, ('field') ArrayObj["field2"] = "field2"; =>index is text here as well another example: for(field in object) =>'field' in this case is the index | | object[field]; =>returns value of 'field' index of object array Event driven programming: JavaScript is used for many evnet driven applications Other event functions: OnChange =>for any change in a specific field/document OnClick =>for a mouse click OnBlur =>when focus is removed from an object/input area OnFocus OnLoad =>when a page/doc loads OnMouseOver OnSelect =>radio buttons or check boxes OnSubmit =>get/post data, etc OnUnLoad =>unload page or document