JavaScript Handout PF
JavaScript Handout PF
Server Side
Search Engine
Log Usage
Gather Data/Stats
Interface to Databases
Many more suitable applications ....
!!
""
!
"
Back
Back
Close
Close
570
Very nice.
Static Pages:
571
572
Writing to Frames
!!
""
!
"
Back
Back
Close
Close
Before delving into the programming there are two more topics to
introduce.
The Document Object Model (DOM) :
573
574
DOM
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
575
It is simply a representation.
!!
""
!
"
<html>
<head>
<title>TITLE OF PAGE</title>
</head>
<body>
<p>Some text...</p>
<ul>
<li>First
<li>Second
</ul>
</body>
</html>
576
!!
""
!
"
Back
Back
Close
Close
JavaScript
578
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
JavaScript is:
Advantages:
579
580
Disadvantages:
!!
""
!
"
Back
Close
The version of JavaScript that was used as the basis of the ECMA
Script standard was 1.1. Therefore everything that is written using
JavaScript 1.1 should comply with the standard.
!!
""
!
"
Back
Close
Learning JavaScript
Borrowing Code
Running Javascript
581
582
to you.
Therefore:
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
Well list the code first then explain whats going on.
<html>
<head>
<title> Hello World JavaScript</title>
</head>
</body>
</html>
584
<body >
<script language="javascript">
</script>
<script language="javascript">
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
You may freely write JavaScript in any place in your Web page.
585
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
The operation of the program is essentially the same as the first version
except that
<html>
<head>
<script language="javascript">
We write a function
587
function init(){
var str = "Hello World";
onLoad="init()"
</body>
</html>
588
We call the function when the Web page loads with the
<body onLoad="init()">
init()
to do the job.
window.alert(str);
</script>
</head>
586
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
More Functions
The program also introduces some more objects and some useful
applications of JavaScript:
589
590
The
Safari Version
!!
""
!
"
Explorer Version
!!
""
!
"
Back
Back
Close
Close
function popup(){
var major = parseInt(navigator.appVersion);
var minor = parseFloat(navigator.appVersion);
var agent = navigator.userAgent.toLowerCase();
592
document.write("<h1>Details in Popup</h1>");
window.alert(agent + " " + major);
}
!!
""
!
"
Back
Close
function farewell(){
window.alert("Farewell and thanks for visiting");
}
</script>
</head>
<body onLoad="popup()" onUnLoad="farewell()">
</body>
</html>
!!
""
!
"
Back
Close
!!
""
!
"
Browsers details:
594
!!
""
!
"
Back
Back
Close
Close
595
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
596
<!-- Begin
function whatBrowser() {
document.Browser.Name.value=navigator.appName;
document.Browser.Version.value=navigator.appVersion;
document.Browser.Code.value=navigator.appCodeName;
document.Browser.Agent.value=navigator.userAgent;
}
// End -->
</SCRIPT>
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
What if you have a good script and you wish/need to use it in several
distinct pages?:
598
600
Good Idea:
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
Variables
<html>
<head>
<title>A Sample JavaScript File Include</title>
<script language=javascript src="sample.js">
</script>
</head>
<body>
...
</body>
</html>
601
!!
""
!
"
!!
""
!
"
Back
Close
Manipulating Variables
Having defined it you can manipulate variables in a variety of
ways.
603
604
num = 23;
str = "Some words";
another_str = str;
first_boolean = true;
Back
602
Close
Strict rules governing how you name your variables (Much like
other languages):
Creating Variables
!!
""
!
"
String concatenation +:
str = str + another_str;
!!
""
!
"
Back
Back
Close
Close
Strings :
String Definition:
Numeric :
605
Perl).
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
Boolean :
Null :
606
"David",
"David Marshall"
"12345.432".
Global :
607
Local :
608
way.
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
Comments
function test {
// this function has a local variable
// a global value is copied into it
// the local is then passed into a
// function as a parameter
610
For example:
// this is a JavaScript comment
Comments continue from that point (//)to the end of the line.
All code form // to end of line are ignored by Javascript
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
Objects in JavaScript
JavaScript attempts to be an object-oriented (OO) language:
634
Some methods.
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
JavaScript Objects
studying the next few slides, or coding (or use a similar object
properties listing)
We do not really need to understand the ins and outs of objects for
this course.
We essentially just use built-in JavaScript objects such as:
635
636
!!
!!
""
!
"
Document and
Window
You can create your own JavaScript objects but well leave this for
another course (or see a good book or Web Site)
http://www.physiol.ox.ac.uk/Computing/Online_Documentation/JavaScript/
""
!
"
Back
Back
Close
Close
Document Properties
The document
Displayed or
Created.
638
!!
""
!
"
Back
Back
Close
Close
bgcolor, fgcolor :
E.g.:
640
document.bgcolor = "#e302334";
document.fgcolor = "coral";
document.write("<body>");
document.writeln("<h1>A test</h1>");
writeln causes a newline to be output with the output.
!!
""
!
"
Back
Back
Close
Close
anchors :
!!
""
!
"
641
642
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
forms :
643
layers :
644
close() :
!!
""
!
"
If you dont use this method then the browser will keep waiting
for more data even if there is none and may not complete
the rendering of the Web page.
!!
""
!
"
Back
Back
Close
Close
open :
645
646
!!
""
!
"
!!
""
!
"
You can also manipulate the returned object, win in your JavaScript
code..
Back
Back
Close
Close
scroll(co-ord, co-ord) :
647
toolbar=[1|0]
location=[1|0]
directories=[1|0]
status=[1|0]
menubar=[1|0]
scrollbars=[1|0]
resizable=[1|0]
Resizing a Window :
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
You may choose to open it at and set its properties very easily in
JavaScript, for example:
648
submission.
649
650
Form Elements:
though JavaScript.
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
<html>
<head>
<script language="javascript">
function validate() {
var method = document.forms[0].method;
var action = document.forms[0].action;
var value = document.forms[0].elements[0].value;
If the name entered in the forms first element (the name text
input field) is not David the form is reset!!!
651
652
if(value != "David"){
document.forms[0].reset();
}
else {
alert("Hi David!!");
!!
""
!
"
Back
}
}
</script>
</head>
<body>
<form method="post">
<input type="text" name="user" size=32>
<input type="submit" value="Press Me!"
onClick="validate()">
</form>
</body>
</html>
Close
Validate/Submit to php/perl
This event is set in the forms input tag for the submit type.
653
!!
""
!
"
Back
Close
Back
Close
!!
""
!
"
<html>
<head>
<script language="javascript">
<!-function verifyForm(theForm) {
if(theForm.username.value == "" ) {
alert("Please enter a name");
return false;}
if(theForm.address.value == "" ) {
alert("Please enter an address");
return false;}
}
//-->
</script>
</head>
<body>
<form name="myForm" method="POST"
654
!!
""
!
"
Back
Close
action="processForm.php"
onSubmit="return verifyForm(this)">
Name: <input type="text" name="username"><br>
Address:<input type="text" name="address"><br>
<input type="submit" value="Send">
</form>
</body>
</html>
}
Now you can only visit processForm.php if David is entered.
655
656
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
More Events
Selected Events
Some events have already been seen i.e. onLoad, onClick, onReset
actions
657
658
another element is selected) with the mouse (used with the input,
select, textarea and button elements).
different browsers.
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
660
!!
""
!
"
Back
Close
Rollovers
Rollover Example
Close
661
!!
""
!
"
<head>
<title>Rollovers</title>
</head>
<body>
<a href="home.html"
onMouseOver="document.homeImage.src=home2.png"
onMouseOut="document. homeImage.src=home1.png">
<img src="home1.png" name="homeImage"
alt="home" border="0" width="100">
</a>
</body>
662
!!
""
!
"
Back
Back
Close
Close
JavaScript Examples
Rollovers
Manipulating Windows
663
!!
""
!
"
664
!!
""
!
"
Back
Back
Close
Close
toolbar=[1|0]
location=[1|0]
directories=[1|0]
status=[1|0]
menubar=[1|0]
scrollbars=[1|0]
resizable=[1|0]
width=pixels
height=pixels
665
<html>
<head>
<script language=javascript>
<!-function Load(url){
666
!!
""
!
"
Back
Close
<body>
<a href=""
onClick="Load(http://www.cs.cf.ac.uk)">
New page</a>
</body>
</html>
!!
""
!
"
Back
Close
The URL can be replaced with empty quotes to open a blank window,
E.g.:
667
668
open("","blankwin);
It cannot be resized
!!
""
!
"
Back
Back
Close
Close
<html>
<head>
<script language=javascript>
<!-function Load(url){
newwin = open(url, "newwin",
status=0,toolbar=0,
resizable=0,
width=500,
height=700);
}
//-->
</script>
</head>
!!
""
!
"
669
Prompts
670
Confirms
Alerts
<body onLoad="Load(ad.html)">
<h1> MAIN
</body>
</html>
PAGE HERE</h1>
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
confirm("string ") :
Shows a window containing a message and two buttons: OK and
Cancel.
E.g. confirm("Please Confirm?");
671
672
!!
""
!
"
Sample applications:
!!
""
!
"
Back
Back
Close
Close
alert("string ") :
Displays the text string and an OK button.
E.g. alert("Are You Alert?");
674
!!
""
!
"
!!
""
!
"
Back
Back
Close
Close
676
!!
""
!
"
Back
Back
Close
Close
677
function Init() {
document.Browser.Name.value=navigator.appName;
document.Browser.Version.value=navigator.appVersion;
document.Browser.Code.value=navigator.appCodeName;
document.Browser.Agent.value=navigator.userAgent;
if (navigator.appName == "Microsoft Internet Explorer") {
alert("You are using Microsoft Products. GO AWAY!!!");
status = "You are using Microsoft Products. GO AWAY!!!";
}
else {
status = "You are using Safari. HOORAY";
}
}
</script></head>
!!
""
!
"
<html> <head>
<title>
Browser Check - Java Script E.G.
</title>
<SCRIPT LANGUAGE="JavaScript">
!!
""
!
"
Back
Close
678
!!
""
!
"
Back
Close
More Examples
Lots more at:
http://javascript.internet.com
679
!!
""
!
"
Back
Close