CS1113 Web Programming: String Manipulations & Animated Images
CS1113 Web Programming: String Manipulations & Animated Images
Lecture 17
String Manipulations & Animated
Images
(JavaScript - VII)
1
Today’s Goal
(String Manipulation and (Images
& Animation)
• To become familiar with methods used for
manipulating strings
name = “BHOLA” ;
document.write( “The length of the
string ‘name’ is ”, name.length ) ;
5
Let us now revisit an example that we
first discussed in the 11th lecture
6
7
<HTML>
<HEAD>
<TITLE>Send an eMail</TITLE>
<SCRIPT type=“text/javascript”>
function checkForm( ) { … }
</SCRIPT>
</HEAD>
<BODY bgcolor=“#FFFFCC”>
<TABLE><FORM …>…</FORM></TABLE>
</BODY>
</HTML> 8
<TABLE>
…
<FORM …>
<INPUT
type=“submit”
name=“sendEmail”
value=“Send eMail”
onMouseOver=“checkForm( )”
>
…
</FORM>
</TABLE> 9
This is a
string
function checkForm( ) {
if( document.sendEmail.sender.value.length < 1 ) {
window.alert(
“Empty From field! Please correct” ) ;
}
}
10
Other Uses of the ‘length’ Property
• ???
11
String Methods
FORMAT
string.methodName( )
EXAMPLE:
name = “Bhola” ;
document.write( name.toUpperCase( ) ) ;
document.write( name.bold( ) ) ;
BHOLABhola
12
Two Types of String Methods
1. HTML Shortcuts
2. All Others
13
String Methods: HTML Shortcuts
sub( ) fixed( )
sup( ) fontcolor( color )
14
big( ), small( ), fontsize( n )
person = "Bhola" ;
document.write( person ) ;
document.write( person.big( ) ) ;
document.write( person.small( ) ) ;
document.write( person.fontsize( 1 ) ) ;
document.write( person.fontsize( 7 ) ) ;
BholaBholaBholaBhola Bhola 15
sub( ), sup( )
person = "Bhola" ;
document.write( person ) ;
document.write( person.sub( ) ) ;
document.write( person ) ;
document.write( person.sup( ) ) ;
Bhola
Bhola Bhola
Bhola
16
bold( ), italics( ), strike( )
name = "Bhola" ;
document.write( name ) ;
document.write( name.bold( ) ) ;
document.write( name.italics( ) ) ;
document.write( name.strike( 1 ) ) ;
BholaBholaBholaBhola
17
fixed( ), fontcolor( color )
person = "Bhola" ;
document.write( person ) ;
document.write( person.fixed( ) ) ;
document.write( person.fontcolor( “green” ) ) ;
document.write( person.fontcolor( “orange” ) ) ;
BholaBholaBholaBhola
18
link( URL )
hotel = "Bhola Continental" ;
document.write( hotel ) ;
document.write( hotel.link(
“http://www.bholacontinental.com” ) ) ;
20
big( ) <BIG> … </BIG>
small( ) <SMALL> … </SMALL>
sub( ) <SUB> … </SUB>
sup( ) <SUP> … </SUP>
bold( ) <B> … </B>
italics( ) <I> … </I>
strike( ) <S> … </S>
21
fontsize( n ) <FONT size=n> …
</FONT>
fontcolor( color ) <FONT color=color> …
</FONT>
fixed( ) <PRE> … </PRE>
link( URL ) <A href=URL> …</A>
22
String Methods: All Others
toLowerCase( ) charAt( n )
toUpperCase( ) substring( n, m )
indexOf( substring, n )
lastIndexOf( substring, n )
split( delimiter )
23
toLowerCase( ), toUpperCase( )
person = "Bhola" ;
document.write( person ) ;
document.write( person.toLowerCase( ) ) ;
document.write( person.toUpperCase( ) ) ;
BholabholaBHOLA
24
charAt( n )
Returns a string containing the character at
position n (the position of the 1st character is 0)
mister = "Bhola" ;
document.write( mister ) ;
document.write( mister.charAt( 0 ) ) ;
document.write( mister.charAt( 8 ) ) ;
document.write( mister.charAt( 2 ) ) ;
Bo
25
substring( n, m )
Returns a string containing characters copied
from positions n to m - 1
s = "Bhola" ;
document.write( s.substring( 1, 4 ) ) ;
document.write( s.substring( 0, s.length ) ) ;
holBhola
26
indexOf( substring, n )
Returns the position of the first occurrence of
substring that appears on or after the nth position,
if any, or -1 if none is found
s = "Bhola" ;
document.write( s.indexOf( “ola”, 1 ) ) ;
document.write( s.indexOf( “z”, 3 ) ) ;
2-1
27
lastIndexOf( substring, n )
Returns the position of the last occurrence of
substring that appears on or before the nth
position, if any, or -1 if none is found
s = "Bhola" ;
document.write( s.lastIndexOf( “ola”, 5 ) ) ;
document.write( s.lastIndexOf( “b”, 0 ) ) ;
2-1
28
split( delimiter )
Returns an array of strings, created by splitting
string into substrings, at delimiter boundaries
23.141592653589793
document.write( "Yes" + false ) ;
Yesfalse
32
Strings In Mathematical Expressions
When a string is used in a mathematical context,
if appropriate, JavaScript first converts it into a
number. Otherwise, a “NaN” is the result
EXAMPLE:
Convert 100.553478 into a currency format
a = 100.553478 ; 100.55
b = a.toString( ) ;
decimalPos = b.indexOf( ".", 0 ) ;
c = b.substring( 0, decimalPos + 3 ) ;
document.write( c ) ;
34
Conversion from Strings
parseInt( ) and parseFloat( ) methods
35
Images & Animation
36
Images in HTML
• It is quite straight forward to include gif and jpg
images in an html Web page using the <IMG>
tag
37
38
<HTML><HEAD>
<TITLE>Image Demo</TITLE>
</HEAD><BODY>
<H1>Image Demo</H1>
• Methods: None
41
The Image Pre-Loading Process
1. An instance of the Image object is created
using the new keyword
44
<HTML>
<HEAD>
<TITLE>Roll the Die</TITLE>
<SCRIPT>
JavaScript Code
</SCRIPT>
</HEAD>
<BODY >
HTML Code
</BODY>
</HTML>
45
<IMG name="pasa" src=“pasa6.gif">
<FORM>
<INPUT type="button" value="Roll the Die"
onClick="rollDie( )">
</FORM>
46
pasaImg = new Array( 7 ) ;
function rollDie( ) {
pasaN = Math.ceil( 6 * Math.random( ) ) ;
document.pasa.src = pasaImg[ pasaN ].src
;
} 47
Where Else Can We Use This?
• ???
48
Animation Example 1
• Take 16 images and cycle through them to
create an animation effect
49
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
50
51
<HTML>
<HEAD>
<TITLE>Animation 1</TITLE>
<SCRIPT>
JavaScript Code
</SCRIPT>
</HEAD>
<BODY >
HTML Code
</BODY>
</HTML>
52
setTimeout( ) executes
circulate( ) once after a
delay of gap milliseconds
<CENTER>
</CENTER>
53
gap = 100 ;
imageN = 1 ;
document.circle.src =
circImg[ imageN ].src ;
imageN = imageN + 1 ;
55
56
Animated Gifs
• We could have saved the 16 gif images of the
previous example in a single file in the form of
an animated gif, and then used it in a regular
<IMG> tag to display a moving image
57
Animation Example 2
• Take 16 images and cycle through them to
create an animation effect
58
59
<HTML>
<HEAD>
<TITLE>Animation 2</TITLE>
<SCRIPT>
JavaScript Code
</SCRIPT>
</HEAD>
<BODY >
HTML Code
</BODY>
</HTML>
60
<CENTER>
<IMG name="circle" src="circle1.gif"
onLoad="setTimeout( 'circulate( )', gap )">
</CENTER>
<FORM>
<INPUT type="button" value="Slow Down"
onClick="slowDown( )">
<INPUT type="button" value="Speed Up"
onClick="speedUp( )">
</FORM>
61
No
gap = 100 ;
change
imageN = 1 ;
imageN = imageN + 1 ;
63
function slowDown( ) { Two new
functions
gap = gap + 20 ;
if( gap > 4000 )
gap = 4000 ;
}
function speedUp( ) {
gap = gap - 20 ;
if( gap < 0 )
gap = 0 ;
} 64
65
Flash Animation
• Designed for 2-D animations, but can be used
for storing static vector-images as well
• Binary-file storage
66
Structured Vector Graphics
• New format; may become more popular than
Flash
• Plug-in required
67