Java Script Instructor: Roseann Krane
What is JavaScript?
It is a scripting language. Programming languages are controlled by a programmer and convert from a human-readable form into a machine-readable form before the program runs by a highly specialized piece of software called a compiler. With a scripting language there is no need to explicitly invoke the code conversion process because it happens automatically in the background when the source code is processed by the target program. A web browser interrupts and dynamically converts the code line by line at run time.
Scripting languages are compact, easy-to-use program-development languages. A short, simple block of scripting code (JavaScript, for example) can almost do the same thing as a much longer, more complex block of standard programming code. There are tradeoffs, such as power, as scripting languages have less functionality.
Netscape developed "LiveScript" to enable HTML authors to enhance their pages and provide basic user interaction. Sun joined this effort through their work with the Java programming language and the new product became JavaScript. JavaScript is capable of detecting various conditions in the current operating environment and reacting accordingly.
JavaScript resembles Java in syntax and use of objects. JavaScript is a distinct program-development language. Both are similar in syntax to C and C++ languages. JavaScript uses built-in objects. Java uses object classes and inheritance. JavaScript code is embedded in HTML page and Java code is in a separate file. JavaScript is interpreted and executed on client machines. Java is compiled on a server and executed on a client computer. Java has strong typing and variables which must be declared and JavaScript has loose typing and variables are not declared.
Some useful tasks for JavaScript:
How does it work?
It is very easy for a Web browser to detect if a HTML page contains embedded JavaScript code. Just use the <SCRIPT> tag to mark the beginning of a JavaScript section and then use the </SCRIPT> tag to indicate the end of that section. Everything between the tags will be interpreted as JavaScript source code rather than standard HTML text. The browser converts the script into its equivalent machine-readable form called binary code and that is executed and the result is inserted into HTML text stream and displayed as if it has been typed into the original HTML document by a programmer.
You create a JavaScript script by inserting JavaScript code into an HTML document. You can insert a block of code by enclosing it between tags as shown below:
<SCRIPT LANGUAGE="JavaScript">
code block ....
</SCRIPT>
Or you can embed JavaScript code within an HTML tag. For example, the following bolded JavaScript code is embedded within a tag:
<BODY BGCOLOR = "black"
TEXT="white"
onLoad= "window.defaultStatus='Home Frame'">
JS#1assignment, part 1: Add the following
to your web page:
<SCRIPT LANGUAGE="JavaScript">
lastModDate = new Date(document.lastModified)
document.write("Last updated: " + lastModDate)
</script>
JS#1assignment, part 2:
Now modify the date script so that it is compact, italicized, and has mm/dd/yy format.
Example to insert: lastModMonth = lastModDate.getMonth() + 1
and you'll need a line for Date and Year. You can hide the code with commands like this:
<SCRIPT LANGUAGE="JavaScript">
<!-- hide JS code
...... something here
// end JS hide -->
</script>
Hopefully you noticed the // preceding the last line to be hidden ending
with the -->. The idea is to hide the javascript from browsers that are
not capable of utilizing the javascript. But some browsers won't
understand the hide marks used in HTML within JavaScript unless you
include the // at the beginning of the last hidden marked line.
End of lesson 1.
JS#1assignment, part 3:
Questions (E-mail the answers to the teacher, put "JS1 Questions" in the subject line!):