























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























-^
Throughout the semester we have always validated our XHTML documents againstthe strict data type definition (Strict-DTD) to ensure that our XHTML documentswere well-formed.
-^
Some JavaScript statements contain symbols such as the less-than symbol (<), thegreater-than
symbol
(>),
and
the
ampersand
(&).
As
you
become
a^
more
sophisticated JavaScript programmer, you will begin to use many of the featurescontained in the JavaScript language and will undoubtedly encounter the need touse these symbols. Unfortunately, these symbols can prevent XHTML documentsfrom passing validation (particularly under the Strict-DTD).– Note that there is less of a problem with this when using the Transitional-DTD, but wedo not want to relax our standards.
-^
This is not a problem at all when using HTML, because any statements inside a
-^
In contrast, with XHTML documents, the statementsin^
a^
Writing Valid JavaScript Code
Writing Valid JavaScript Code
-^
While JavaScript libraries are quite common, it isalso
quite
common
to
see
both
libraries
and
embedded JavaScript code in Web documents, so youneed to be familiar with both forms.
-^
Recall that the
/*^ This
is^ a
JavaScript
library
of^ scripts
//this
script
writes
the^
date^
and^ time
onto
a^ page
document.write("
Today
is:^
"^
+^ Date()
+^ "
");document.write("^ 11){document.write("PM") }^ else
{ document.write("AM") } document.write("^ Hello
there!
I'm
a^ JavaScript
script
executing
on^ your
behalf.
Execution of SCRIPT #1Execution of SCRIPT #2Execution of SCRIPT #3Execution of SCRIPT #4We’ll see shortly how touse functions to “call” andexecute only one of thesescripts or change theorder of execution orwhatever we want to dowith them.
-^
The values a program stores in computer memory arecalled variables.
Technically speaking, a variable is
actually a specific address in the computer memory.The data stored in a variable often changes.– Think of a variable like a book bag or back pack. You canput any book you want in the bag and retrieve it later foruse. The books in your bag this semester will probably notbe the same ones in your bag next semester.
-^
Many programming languages, such as Java and C++have a very large set of rules that apply to variables.JavaScript is very loose in how variables can be used.
More JavaScript - Variables
Reserved Words In JavaScript
-^
Before you can use a variable, you need to declare it (basicallyit means create it).
While there are different techniques to
create
variables,
we’ll
stick
with
the
most
common
and
simplest form which uses the reserved word
var
.
-^
For example, to create a variable named
myVariable
, you
need to write this statement:
var myVariable;
-^
All this statement does is make some memory location beaccessible to your code whenever you refer to it by this name.
Using Variables
myVariable
computer memory
-^
While some variables will be used only internally tothe JavaScript, others will need to be sent to the Webpage for display.
-^
This is quite simple in JavaScript and is basically nodifferent than what we have already been doing withour
simple
scripts
that
have
been
printing
text
(strings) to the Web page.
-^
Writing A Variable’s Value To A Web PageThe next page illustrates a simple XHTML documentwith an embedded JavaScript that uses a variablewhich is sent to the Web page for rendering by thebrowser.
Writing A Variable’s Value To A Web Page - Example Writing a JavaScript variable to a Web page