Javascript
CSc 302 Outlines
Forms
Random Stuff
What is Javascript?
Programming language inside web pages.
Interpreted by the browser.
Uses.
Interact with the user.
Check data before sending: Reduces net traffic.
Problems.
Not all browsers run javascript.
Not all browsers have it turned on.
Behavior of some constructs varies.
Make sure you page does not depend on Javascript to work.
Javascript is not java.
Javascript Furniture
Numbers and operations:
5 + 6
Strings:
"Hi there"
or
'Hi there'.
String concatenation:
"Left" + "Right"
is
"LeftRight"
Variables:
var z = 17
Objects: Things you can talk to and use.
Window, Frame, Document, etc.
Parts of each other.
Objects have attributes. Examples:
window.status
document.bgcolor
history.previous
Attributes may be queried or changed.
Parts are named by walking down from the top using dot:
window.history.length
.
Window
Frame
Document
Location
History
Layer
Link
Image
Area
Anchor
Applet
Plugin
Form
Text Area
Text
File Upload
Password
Hidden
Submit
Reset
Radio
Checkbox
Button
Select
Option
Object Hierarchy: Objects Are Parts Of Others
Inserting Javascript.
Contents of the
script
tag are run when the page is loaded:
<script language="javascript">
window.alert("Hi there");
</script>
Code may be located in another file:
<script language="javascript" src="somewhere.js">
</script>
Code may be contained in event attributes, and is run when the user does something:
onclick
,
onchange
,
onload
, etc.
<input type="text" size="5"
onchange="window.alert('Hi there');" />
Some examples:
Message On Load
Click To Raise
Uses an onClick on a div.
Changes the z-index when clicked.
Change Colors
.
This uses a form and the
<input type="button" />
tag.
The form is not submitted; it just for scripting.
The
type=button
is only for scripting.
Uses
onclick
to run code when a button is pressed.
Changes the bgColor attribute of the document.
Self-Writing Document
.
Arithmetic
.
Uses events to perform arithmetic.
The
id
attribute is used to identify fields.
The need for
parseFloat
is due to some Javascript idiocy.
Colors, Again
.
This one sets any background color.
It uses a function and simple a array.
Notice the re-initialization code at the bottom. I needed this to set the page on a reload.
Automatic Color Changes
This uses
setTimeout
to run some code later.
The various functions call each other in rotation.
Also uses the
window.status
to change the status line at the bottom of the window.
Gradual Color Change
This uses
setTimeout
to run some code later.
Does some interesting conversions to change the color up and down.
Popups
Button generates a popup menu from javascript.
The window which pops up has javascript button to close it.
Many options for the popup window.
Mouseover
The
onmouseover
and
onmouseout
operate when the mouse enters and leaves an image.
The large image is actually two images, changed using the src property.
The green arrow uses the history object.
Index Pictures
Indexed set of images on a single Javascript-driven page.
Index has a fixed location at the top. Horizontal scroll on the image list.
Main content div is lowered by the height of the index div.
Cursor property to show that thumbs can be clicked.
Clicking uses Javascript to load each main image by changing the the
src
property.
Same Images, Different Code
Only difference: Use a Javascript loop to create the repetitive HTML within the index.
Form Validation
Use the
onsubmit
attribute to check data before sending.
The
verify
function returns true if it approves, or makes an alert and returns false otherwise.
Also makes use of some string functions.
Conditionals.
String handling.
Getting Javascript error messages.
Explorer: Tools/Internet Options/Advanced. Turn on script debugging and display on error.
Mozilla: Tools/Error Console
Not very much from Ch. 15.
Forms
Random Stuff