CPS616 JavaScript 1997 CPS 616 January-April 1997 Computational Science Track on Base Technologies for the Information Age: Module on JavaScript See: http://www.npac.syr.edu/users/gcf/cps616javascript97 Instructor: Geoffrey Fox teamed with Meryem Ispirli, Nancy McCracken, Tom Scavo, John Yip Syracuse University 111 College Place Syracuse New York 13244-4100 Abstract of CPS616 JavaScript Presentatation Java versus JavaScript versus HTML Issues Some Simple Motivating Examples Language Features and Syntax The Peculiar Object Model -- Functions Properties and Methods Arrays in old and new JavaScript Arguments of Functions with and other object related syntax The JavaScript built in Math String and Date Objects The Navigator Objects -- Frames Windows Document Form Event Handling including Function object Cookies User Objects and Examples including using JavaScript for frames General Remarks on JavaScript - I Historically JavaScript was called LiveScript and developed by Netscape with some of the same goals as Java but focussed on a "smaller world" -- manipulation of text and options connected with Netscape Clients TEXT ............................................. COMPUTER Language HTML --> JavaScript Java <--- C++ Now we can use it under some circumstances as an alternative to Java where the "rapid prototyping" of a fully scripted language is helpful We expect somebody to develop a fully interpreted Java -- equivalently to extend JavaScript to support more built-in objects (e.g. networking, images) JavaScript is "either" active text (HTML) or a focused language! JavaScript can be thought of as Java with the AWT (Abstract Windowing Toolkit) replaced by Netscape Client JavaScript is a domain specific Interpreter for "Web Document Domain" e.g. Postscript is a domain specific Interpreter for text layout and MATLAB is an interpreter for matrix manipulation, Mathematica is domain specific for mathematics ...... General Remarks on JavaScript - II JavaScript particularly useful for multi-frame windows and for manipulating forms without complex CGI (Server Side) scripts Here it can be much faster as purely client side whereas a CGI based form has inevitable delays as requires server-client communication and invocation of a new process on server side. Java shares this advantage This client side processing speed can be essential in some applications as users would not accept long delays (several seconds) on "trivial" mouse clicks Java is a broad industry standard with a careful well documented design JavaScript comes from Netscape but is supported by Microsoft. It has a poorly documented (and thought through ?) basic design Java / Interpreter Structure Typical Java Applet Structure Java Source ---> javac compiler --> JavaVM Universal machine code in .class file Store JavaVM .class files on Web Server Download JavaVM from Server to Client. Interpreter built into browser, reads JavaVM and executes on client Typical JavaScript Structure JavaScript source is included in HTML text Combined JavaScript and HTML is downloaded and interpreted by browser on client to produce HTML page Particular user actions on HTML page (e.g. mouse clicks and form entries) can invoke JavaScript functions on the client As browser one must "understand" raw high level language, this is substantially slower than interpreting JavaVM where "just" need to map universal machine code into machine code for a particular computer Note in each case , Java and JavaScript use "runtime" which is "compiled high performance library" (probably written in C and C++) Some Performance Comments Execute C code Instruction -- 1 clock cycle ( 10^8 per second) Run a client side C subroutine -- can do upto 10^6 instructions in time (few milliseconds) that is significant for user JavaVM Interpreter -- roughly 50 times slower than C JavaVM Just in Time Compiler (effectively compile JavaVM in real time) -- roughly 2 to 8 times slower than C Native Java Compiler -- same speed as C Perl Interpreter (but does process whole program before interpreting) -- 500 times slower than C except in runtime Tcl, JavaScript "true" Interpreters - 5000 times slower than C Can still do 100-1000 instructions in time that users don't notice! Contact Host via CGI script -- Server, Network and Client HTML page regeneration delay can be measured in seconds. We need to know HTML! Note like most interpreters (look at Basic manual) JavaScript has a huge and growing number of "basic" methods and objects To study JavaScript, we will have to use some relatively advanced HTML features including Frames Forms -- especially event handlers Cookies Hello World Example of JavaScript- I