1. Sukrit
[email protected] 2. • Invented by Brendan Eich at Netscape, in1995.• JavaScript is a programming language designed for Web pages.• JAVASCRIPT is used to maintain BEHAVIOR of the document.• JavaScript is used in millions of Web pages to improve the design, validate forms, detect browsers, create cookies, and much more.• JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Mozilla, Firefox, Netscape, Opera.• JavaScript is an interpreted language (means that scripts execute without preliminary compilation)• Everyone can use JavaScript without purchasing a license• Unlike HTML, JavaScript is case sensitive. 3. Requires the JDK to create Requires a text editor the applet Required a browser that can Requires a Java virtual interpret JavaScript code machine to run the applet JavaScript can be placed Applet files are distinct from the HTML code within HTML and XHTML Source code is hidden from Source code is made the user accessible to the user Programs must be saved as Programs cannot write separate files and compiled content to the hard disk before they can be run Programs run on the client Programs run on the server side side 4. • JavaScript code is included within tags: • document.write("Hello World!") ; • Notes: • The type attribute is to allow to use other scripting languages (but JavaScript is the default) • This simple code does the same thing as just putting Hello World! in the same place in the HTML document • The semicolon at the end of the JavaScript statement is optional. • We need semicolons if you put two or more statements on the same line. 5. • JavaScript can be put in the or in the of an HTML document • JavaScript functions should be defined in the • This ensures that the function is loaded before it is needed • JavaScript in the will be executed as the page loads• JavaScript functions can be put in a separate .js file • • Put this in the • An external .js file lets us use the same JavaScript on multiple HTML pages • The external .js file cannot itself contain a tag• JavaScript can be put in an HTML form Object, such as a button • This JavaScript will be executed when the form object is used 6. • Some old browsers do not recognize script tags • These browsers will ignore the script tags but will display the included JavaScript • To get old browsers to ignore the whole thing, use: • The , the // starts a JavaScript comment, which extends to the end of the line• Some users turn off JavaScript • Use the message to display a message in place of whatever the JavaScript would put there 7. Manipulating HTML Elements Writing to The Document Output• My First • My First Web Page Paragraph document.write("My First document.getElementById("demo" JavaScript"); ).innerHTML="My First JavaScript"; 8. • Variables are declared with a var statement: • var pi = 3.1416, x, y, name = "Dr. Dave" ; • Variables names must begin with a letter or underscore • Variable names are case-sensitive • Variables are untyped (they can hold values of any type) • The word var is optional Variables declared within a function are local to that function (accessible only within that function)• Variables declared outside a function are global (accessible from anywhere on the page) 9. • Dynamic Typing - Variables can hold any valid type of value:– Number … var myInt = 7;– Boolean … var myBool = true;– Function …var fname= function() { ……. };– Object …... var person={firstname:"John", lastname:"Doe", id:5566};– Array ……. var myArr = new Array();– String …….var myString = ―abc‖;– … and can hold values of different types at different times during execution. 10. Arithmetic Operators Assignment Operators 11. Comparison Operators Logical Operators 12. Alert Box Confirm Box A confirm box is often used if we An alert box is often used if we want the user to verify or accept want to make sure information something. comes through to the user. When a confirm box pops up, the When an alert box pops up, the user will have to click either "OK" or user will have to click "OK" to "Cancel" to proceed. proceed.alert("Hello World!") If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. • confirm(―Sure??") 13. • Prompt Box • A prompt box is often used if we want the user to input a value . • When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. • If the user clicks "OK―, the box returns the input value. If the user clicks "Cancel―, the box returns null.• var name=prompt("Please enter your name","Harry"); 14. • „if‟ statement • if ( boolean statement ) {…} else {…}• „switch‟ statement • switch ( myVar ) { • case 1: // if myVar is equal to 1 this is executed • break; • case “two”: // if myVar is equal to “two” this is executed • break; • case default: // if none of the cases above are satisfied OR if no • // „break‟ statement are used in the cases above, • // this will be executed • } 15. • The For Loop • SYNTAX: for (start; condition; update) {JavaScript Commands} • start is the starting value of the counter. • condition is a Boolean expression that must be true for the loop to continue • update specifies how the counter changes in value each time the command block is executed • Example for (var i=0; i