1 / 14

Javascript

Javascript. CS 236369, Spring 2010. What is Javascript ?. Browser scripting language Dynamic page creation Interactive Embedded into HTML pages Javascript is NOT Java Different syntax Different programming concepts Javascript runs on client-side (browser) only.

belita
Download Presentation

Javascript

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. Javascript CS 236369, Spring 2010

  2. What is Javascript ? • Browser scripting language • Dynamic page creation • Interactive • Embedded into HTML pages • Javascript is NOT Java • Different syntax • Different programming concepts • Javascript runs on client-side (browser) only

  3. Adding Javascript to HTML • All Javascript code must be inside <script> • Old browsers do not support Javascript • Example: <html> <script language=“javascript”> <!-- document.write(“<h1>Hello World</h1>”); --> </script> <script language=“text/javascript” src=“myscript.js” /> </html>

  4. Programming concept • No static types • Everything is an associative array • Every variable can hold attributes • No access limitations (i.e no private attributes) • Functions are variables • Dynamic binding • Inheritance is possible (though complicated and incomplete)

  5. Variables example var x = 1; // define a numeral variable x = “123”; // x is now a string x = new Object(); // x is now an object x.attr = “val”; // x now has the attribute ‘attr’ x.f = function() { document.write(“a”); }; // x now has a function named ‘f’ x.f(); // f is invoked

  6. Functions • Defined by the keyword ‘function’ • No return type and no argument types • Can be invoked with any number of arguments • Example: function f(x) { alert(x); } f(‘abc’); // result will be an alert box with ‘abc’ f(); // result will be an alert box with ‘undefined’ f(‘abc’, ‘edf’); // result will be an alert box with ‘abc’

  7. Execution • Javascript code may be executed in 2 different cases: • Upon parsing the page, when the browser encounters javascript code not inside a function Example: <script type=“javascript”> document.write(‘<h1>Hi</h1>’); </script> • Events (next slide)

  8. Events • Javascript can be activated on certain events • When a page is loaded • When a button is clicked • When a form is submitted • And many more … • Example: function f() { alert(‘clicked !’); } <input type=“button” onclick=“f();” value=“go!”/>

  9. Events • Consider the following example <div id=“outer” onclick=“f();”> <div id=“inner” onclick=“g();”>click me</div> </div> • Which javascript function should be invoked first? • Netscape answer: f • Firefox answer: default g, can be changed to f • Microsoft Explorer: g

  10. Events (form submission example) function validate_required(field, alerttxt) {if (field .value==null|| field.value==“”) { alert(alerttxt); return false;}return true; } function validate_form(form) { if (validate_required(form.email,”Email is requred”)==false) { return false; } return true; }

  11. Events (form submission cont) <html> <head> <script type=“javascript” src=“example.js” /> </head> <body> <form action=“submit.jsp” onsubmit=“return validate_form(this);”> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html>

  12. DOM • Javascript has built in access to the html document DOM tree • Every element in the html document can be removed or modified • New elements can be inserted to the html document dynamically

  13. DOM Example <html><head> <script language="javascript"> function f() { var elmt = document.createElement('h1'); var txtNode = document.createTextNode('new element'); elmt.appendChild(txtNode); document.getElementById('someId').appendChild(elmt); } </script></head> <body> <input type="button" onclick="f();" value="click" /> <div id="someId" /></body></html>

  14. References and tutorials • http://www.w3schools.com/jsref/dom_obj_document.asp • http://www.w3schools.com/js/default.asp • http://www.javascriptkit.com/javatutors/closures.shtml • http://www.webreference.com/js/column79/

More Related