JavaScript
JavaScript – the language Web page manipulation with JavaScript and the DOM
2
jonkv@ida
Why JavaScript?
1994‐1995: Wanted interactivity on the web
- Server‐side interactivity: CGI
▪ Common Gateway Interface ▪ Batch‐oriented (fill in a form, get interactive results)
- "Heavy‐weight" client‐side interactivity: Java Applets
▪ Self‐contained part of a web page ▪ Interacts with the user + possibly a server ▪ Does not interact with the document itself ▪ (Interactive versions of Macromedia Flash not available until 1998)
- "Light‐weight" client‐side interactivity: Netscape LiveScript
▪ Script language for the web (introduced late 1995) ▪ Renamed to JavaScript (marketing!) ▪ Standardized version: ECMAscript (ECMA‐262, ISO‐16262) ▪ Also used in Flash, Firefox/Thunderbird UI, PDF files, MacOS Dashboard Widgets, PhotoShop, …
3
jonkv@ida
JavaScript 1: Example
A simple JavaScript example:
- In HTML, scripts are CDATA – not parsed by browsers, validators
▪ <!DOCTYPE HTML PUBLIC …> <HTML> <head> <title>Hello, World!</title> <script type="text/javascript"> alert("Hello, World!"); // Show an alert to the user </script> </head><body>…</body></HTML>
- In XHTML, your inline code is by default PCDATA (parsed)
▪ if (a < b) – the “<“ will be interpreted as the start of a tag ▪ <script type="text/javascript"> /* <![CDATA[ */ … your code … /* ]]> */ </script>
4
jonkv@ida
JavaScript 2: Script files
Better solution: Separate script file
- Test.html:
▪ <!DOCTYPE HTML PUBLIC …> <HTML> <head> <title>Hello, World!</title> </head> <body> <script type="text/javascript" src="example.js"></script> </body> </HTML>
- example.js:
▪ // Show an alert to the user alert("Hello, World!");