CS 110 Fall 2007 Javascript The BROWSER scripting language. Similar syntax to java. History: Netscape created to allow dynamic web pages. Uses: Many, here are a couple: verifying and reacting to user input, updating page without making user wait Javascript tutorial: http://www.w3schools.com/js/js_intro.asp Alert box sample: http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm <html> <head> <script type="text/javascript"> function disp_confirm() { var r=confirm("Press a button"); if (r==true) { document.write("You pressed OK!"); } else { document.write("You pressed Cancel!"); } } </script> </head> <body> <input type="button" onclick="disp_confirm()" value="Display a confirm box" /> </body> </html>CS 110 Fall 2007 Can also put javascript in a file external to the html: <html> <head> <script src="confirm.js"> </script> </head> <body> <input type="button" onclick="disp_confirm()" value="Display a confirm box" /> </body> </html> confirm.js: function disp_confirm() { var r=confirm("Press a button"); if (r==true) { document.write("You pressed OK!"); } else { document.write("You pressed Cancel!"); } } See sample at http://www.cs.usfca.edu/~wolber/jsSamples/confirm2.htmlCS 110 Fall 2007 Advanced Uses DOM – Document Object Model Your program can get at the elements in the html. So, for instance, you can sort a list in different ways. A big data structure representing the html page. AJAX – Asynchronous javascript and xml Call a server to get data, asynchronously. User doesn’t have to wait for call to web service to complete In-Class Assignment: Add some javascript to the homepage you created. Put the javascript in a separate file than the html. Make sure it runs in a browser. If there is a permissions problem, you’ll need to set the permissions on the files you create. One way to do this is, from the console, enter: chmod 0777 file.js. Do it for your html as well. Vacation fun: Write mastermind with html and javascript! Html page Java script External Web service
View Full Document