Internet Systems
67
Chapter 7. JavaScript: Intro. to Scripting
JavaScript was originally developed by Netscape.
Microsoft’s version is JScript.
Microsoft and Netscape have cooperated in the standardization of
JavaScript/JScript by the ECMA known as ECMAScript.
We refer to JavaScript and JScript generically as JavaScript.
We start with JavaScript code in the header – this is executed
before the body.
(We later look at inline scripting, JavaScript code in the body.)
Enclose the code in <script> … </script> with attribute
type with value “text/javascript”.
Single-line comments are introduced with // .
Multi-line comments are enclosed in /* … */ .
The browser contains a complete set of objects (with associated
data and methods) that allow programmers to access and
manipulate every element in an HTML document.
The document object represents the document currently displayed.
Method writeln writes a line of HTML text in the document and
positions the output cursor at the beginning of the next line.
Method write is similar but doesn’t advance the output cursor
to the next line.
These methods write HTML text; the browser must still render
the text.
Internet Systems
68
Example:
(Here the <body> … </body> tags are not needed.)
<html>
<head>
<title>First Java Script Program</title>
<script type = "text/javascript">
document.write( "<H2>Hello " );
document.writeln( "world!</H2>" );
</script>
</head>
<body>
</body>
</html>
Hello world!
Internet Systems
69
Dialog Boxes
Dialog boxes are manipulated with methods of the browser’s
window object.
window.alert(“text”); − an alert dialog
The box is
automatically
sized to
accommodate
text.
Allow the user to
dismiss the dialog box.
Internet Systems
70
Example:
<html>
<head>
<title>Using a dialog box</title>
<script type = "text/javascript">
window.alert( "Hello\nworld!" );
</script>
</head>
<body>
<p>Click Refresh to