JavaScript
The Master Site
Java Tutorial
Alerts
Basics
Commands
Confirmation Boxes
Crappy Passwords
Functions
Input
Loops
Opening New Windows
Prompts
Variables
= = = = Basics = = = =
< SCRIPT language="JavaScript" >
......JavaScript Code......
< /SCRIPT >
NOTE:
Put Functions in the < HEAD > section of the HTML document
Arithmetic
* + - / (Obvious)
% (MOD)
Comparison Operators
< <= > >=
==
(Equal to; = gives something a value)
|=
(Not Equal to)
Logical Operators
&& (And)
|| (Or)
! (Not)
= = = = Variables = = = =
Declare variables in the SCRIPT tag inside of the HEAD tag
Integers and Real variables: var name=value;
Strings: var name="value";
Boolean: var name=true; or false;
= = = = Loops = = = =
FOR LOOP:
for ( condition1; condition2; command)
{
JavaScript Statements....
}
Example:
var count=1;
var mymoney=0;
for (count=1; count<11; count=count+1)
{
mymoney=mymoney+100;
}
WHILE LOOP:
Example:
var count=1;
var mymoney=0;
while (count<11)
{
mymoney=mymoney+100;
count=count+1;
}
= = = = Input = = = =
< FORM >
< INPUT type="button" value="Click Me" name="button1" onClick="document.bg='colour'" >
< /FORM >
< INPUT >
(input of some kind)
type="button"
(declares input as a button)
value="Click Me"
(what is written on the button)
name="button1"
(name for later reference)
= = = = Commands = = = =
onClick=""
(instruction on what to do when something is clicked)
onMouseover=""
(instruction on what to do when cursor placed over something)
onMouseout=""
(instruction on what to do when cursor taken off something)
Examples:
< FORM >
< INPUT type="button" value="Click Me" name="button1" onClick="document.bg='colour'" >
< /FORM >
< A HREF="MEP.html" onMouseover="window.status='Hi there!'; return true" onMouseout="window.status=' '; return true" >
Place your mouse here!< /A >
...STUFF TO DO WITH THESE COMMANDS...
"window.status='Hi there!'; return true"
(tells browser to write 'Hi there!' on the status bar)
"window.status=' '; return true"
(clears the status bar from previous ramble)
"document.bg='colour'"
(changes background colour of screen)
"window.location='URL'"
(sends user to another page)
"window.open('URL','name-for-reference','attribute1,attribute2')"
(opens another page in a new window)
"alert('BLAH BLAH BLAH')"
(makes a little alert box)
= = = = Alerts = = = =
"alert('BLAH BLAH BLAH')"
(makes a little alert box - THIS TO BE USED IN COMMANDS)
To put an alert before a page is open, put it in the < HEAD > section as a JavaScript function
Example (multiple alerts):
< SCRIPT language="JavaScript" >
alert('BLAH BLAH BLAH');
alert('BLAH BLAH BLAH!);
< /SCRIPT >
Alerts can be put in buttons too, ho ho hO!
= = = = Confirmation Boxes = = = =
< HEAD >
< SCRIPT language="JavaScript" >
function go_there()
{
var where_to= confirm("Do you really want to go to this page??");
if (where_to== true)
{
window.location="URL";
}
else
{
window.location="URL";
}
}
< /SCRIPT >
< /HEAD >
= = = = Prompts = = = =
prompt('Question?', '');
ASKING A QUESTION BEFORE SITE OPENS:
< SCRIPT language="JavaScript" >
var yourname= prompt('Please enter your name, so you can get a special greeting', ' ');
if ( (yourname==' ') || (yourname==null) )
{
yourname="Dude";
}
< /SCRIPT >
< /HEAD >
< BODY >
< SCRIPT language="JavaScript" >
document.write("< CENTER >< H1 >Hello, " + yourname + " ! Welcome to My Page! <\/H1><\/CENTER>");
< /SCRIPT >
< /BODY >
Things to Note:
+ yourname +
(so document writes string AND variable AND other string)
<\/H1>
(JavaScript needs this to close tags inside a string)
= = = = Functions = = = =
Inside SCRIPT tag of HEAD tag
function name (parameter1, parameter2)
{
JavaScript statements and declarations
}
TO CALL A FUNCTION...
Enter function name and any input
ex: < INPUT TYPE="button" value="Go!" onClick="go_there()" >
= = = = Crappy Passwords = = = =
< HEAD >
< SCRIPT language="JavaScript" >
var password;
var pass1="word";
var pass2="obusor";
password=prompt('Please enter your password',' ');
if (password==pass1 || password==pass2)
{
alert('Password Correct! Click OK to enter!');
}
else
{
window.location="URL";
}
< /SCRIPT >
< /HEAD >
NOTE:
Script tag inside Head tag OF THE PAGE TO PROTECT
Easy to hack: just disable JavaScript and view sourcecode!
= = = = Opening New Windows = = = =
ATTRIBUTES ETC: Click Here!
So links can open new windows...
In HEAD tag
< HEAD >
< SCRIPT language="JavaScript" >
function newwindow()
{
window.open('MEP.html','window-name-for-future-reference','width=300,height=200,resizable=yes');
}
< /SCRIPT >
< /HEAD >
In LINK tag
< A HREF="javascript:newwindow()" > Click Here! < /A >