There is only one real JavaScript statement here -- namely
|
document.writeln("textstring");
|
This outputs into current page the text in quotes followed by a newline
|
And note the rather peculiar way we "hide" JavaScript from browsers that can't understand it by enclosing in convential HTML Comment syntax
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
...... Bunch of JavaScript Statements .......
|
//scriptend-->
|
</SCRIPT>
|
Note depending on your needs, JavaScript can be in Header or Body section of document
|
<HTML><HEAD><TITLE>Javascript with Forms</TITLE>
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
function compute(form) {
|
if( confirm("Is this what you want?"))
|
form.result.value = eval(form.expr.value);
|
else alert("Enter a new expression then!"); }
|
//scriptend-->
|
</SCRIPT></HEAD>
|
<BODY><FORM>
|
Enter An Expression:
|
<INPUT TYPE="text" NAME="expr" SIZE=15>
|
<INPUT TYPE="button" VALUE="DoIt!" ONCLICK="compute(this.form)">
|
<BR>Result:
|
<INPUT TYPE="text" NAME="result" SIZE=15>
|
<BR>
|
</FORM></BODY></HTML>
|
<HTML><HEAD><TITLE>Javascript with Forms</TITLE>
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
function compute(form) {
|
if( confirm("Is this what you want?"))
|
form.result.value = eval(form.expr.value);
|
else alert("Enter a new expression then!"); }
|
//scriptend-->
|
</SCRIPT></HEAD>
|
<BODY><FORM>
|
Enter An Expression:
|
<INPUT TYPE="text" NAME="expr" SIZE=15>
|
<INPUT TYPE="button" VALUE="DoIt!" ONCLICK="compute(this.form)">
|
<BR>Result:
|
<INPUT TYPE="text" NAME="result" SIZE=15>
|
<BR>
|
</FORM></BODY></HTML>
|
<HTML><HEAD><TITLE>Javascript for Parameterizing HTML</TITLE>
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
var imagewidth=600; // These could be changed by form input or some
|
var imagefile="npac.gif"; // computation based on size of window etc.
|
//scriptend-->
|
</SCRIPT></HEAD>
|
<BODY> ......Bunch of Normal Stuff
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
document.writeln('<img align=top width=' + imagewidth + ' src="' + imagefile + '" >');
|
//scriptend-->
|
</SCRIPT>
|
.... Yet More Normal Stuff
|
</BODY></HTML>
|
Note single quotes used for JavaScript, Double quotes for HTML -- can use \' if necessary to hide special meaning from JavaScript
|
<HTML><HEAD><TITLE>Javascript for Parameterizing HTML</TITLE>
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
var imagewidth=600; // These could be changed by form input or some
|
var imagefile="npac.gif"; // computation based on size of window etc.
|
//scriptend-->
|
</SCRIPT></HEAD>
|
<BODY> ......Bunch of Normal Stuff
|
<SCRIPT LANGUAGE="JavaScript" >
|
<!-- A Conventional comment to hide JavaScript from old browsers
|
document.writeln('<img align=top width=' + imagewidth + ' src="' + imagefile + '" >');
|
//scriptend-->
|
</SCRIPT>
|
.... Yet More Normal Stuff
|
</BODY></HTML>
|
Note single quotes used for JavaScript, Double quotes for HTML -- can use \' if necessary to hide special meaning from JavaScript
|
One can parameterize HTML using document.write(ln) commands such as:
|
document.writeln("string1" + JSvariable +"string2"); // etc.
|
A "lighter weight way of achieving this can be illustrated by direct HTML syntax with NO <SCRIPT> tag and no document.write
|
<IMG WIDTH="&{JSvar1};" SRC="&{JSvar2};" >
|
where JSvar1 and JSvar2 are JavaScript variables holding width and URL of image
|
Syntax is modeled on that for for special characters
|
Such JavaScript Entities can ONLY appear on RIGHT HAND side of attribute values
-
Cannot appear in HTML text such as titles.
-
document.writeln approach has no such restriction!
|
JavaScript: User has full access to capabilities of Netscape Navigator in functionality of window components
|
Java: User relies on Java AWT (or potentially HotJava browser) for features of Window Components
|
JavaScript: No explicit data types (just var) --- loose typing
|
Java: Variable data types must be declared and carefully "cast" to convert -- strong typing
|
JavaScript: Dynamic Binding. Object references are interpreted at runtime (typical interpreter)
|
Java: Static Binding. Object references must exist at compile time.
|
Security: Both JavaScript and Java applets as implemented today have significant security restrictions. This is a (Netscape) Runtime and not a language feature!
|
JavaScript only has one simple type -- namely:
|
var anumber = 137; // or the SAME type var
|
var astring = "1"; // to define a string
|
The loose typing in JavaScript, allows interpreter to creatively interpret an expression and default (this is change from early documentation which claimed type taken from leftmost variable) is that strings have precedence over numbers so that for instance:
|
x = astring + anumber; // results in x = "1137"
|
use parseInt and parseFloat to extract numerical values from strings (see later)
|
Note special value null (no quotes) can be used as a null value
|
Strings can be delimited by '..text..' or "..text.." which are identical in meaning (unlike PERL) and one can use \n for newline and \t for TAB
|
Boolean literals are either true or false
|
Comments are /* any stuff including newlines */ or
|
Javascript statements; // This is a comment until new line
|