22
1 Javascript Examples Discuss regular expressions in Java course, show their use in Javascript J M Githeko

javascript examples

Embed Size (px)

DESCRIPTION

Javascript examples

Citation preview

Page 1: javascript examples

1

Javascript ExamplesDiscuss regular expressions in Java course, show

their use in Javascript

J M Githeko

Page 2: javascript examples

2

Agenda

Dynamic Effects: Button roll-overs Form Validation

Page 3: javascript examples

3

Button Rollovers

Image access using DOM Image properties Image stored on object variables

(preloading) Changing image properties using

event handlers

Page 4: javascript examples

4

Step 1: Creating the Basic Page

<html><head><title>Image Rollover</title></head>

<body bgcolor="white">

<img src="screw.jpg">

</body></html>

Page 5: javascript examples

5

Step 2: Naming the Image

<img src="screw.jpg" name="pic1">

Page 6: javascript examples

6

Step 3: Create Event <a href="" onMouseOver="roll_this() "

onMouseOut=“roll_off()”><img src="screw.jpg" name="pic1" border=0>

</a>

Note: We use an anchor <a> because onMouseOver on an image is not recognized by some browsers.

Note “border=‘0’” to prevent border being shown around the image

Page 7: javascript examples

7

Step 4: Changing the Image “src” property

The image's src property can be referenced in JavaScript with "document.images.pic1.src"

The term "images" refers to all of the images in a document

pic1 is the name of image we shall manipulate

src property is the URL of the image file

We can change the src property

Page 8: javascript examples

8

Step 5: Creating the Functions

<script style = “text/javascript">

function roll_this()

{document.images.pic1.src="capnut.jpg";}

</script>

Page 9: javascript examples

9

Step 5 (cont’d)

function roll_off() {

document.images.pic1.src=“images/screw.jpg";}

Note: Ensure that the image files are accessible in the same directory as the source file or change image url to point elsewhere

Page 10: javascript examples

10

Rollover for a Single Button

<a href=""onMouseOver="document.images.pic1.src='capnut.jpg'"onMouseOut="document.images.pic1.src='screw.jpg'"></a>

Page 11: javascript examples

11

Preloading Images Improves speed of rollover Images stored in image objects Giving pixel dimensions optimises

memory usage Image objects created as follows:

some_name = new image(100, 20);some_name.src = “images/rhino.jpg”;

Page 12: javascript examples

12

Preloading Images (cont’d)<head><script type="text/javascript"><!--

image01= new Image()image01.src="1.gif"image02= new Image()image02.src="3.gif"

//--></script></head>

Page 13: javascript examples

13

Preloading Images (cont’d)More Complex Example

<SCRIPT type = “text/javascript”><!--pic1= new Image(100,25); pic1.src="http://someplace.com/image1.gif"; //--></SCRIPT>

Defines a new image object, width of 100, height of 25 [Replace these with the width and height of your image]

The second defines the url or web address of the image [Replace this with the url of your image]

Page 14: javascript examples

14

Preloaded Images Complete Example (cont’d)

<html><head><script type="text/javascript"><!--image01= new Image(88,31)image01.src=“images/orup.jpg"image02= new Image(88,31)image02.src=“images/ordown.jpg"//--></script></head>

Page 15: javascript examples

15

Preloaded Images Complete Example (cont’d)

<body><a href="“ onmouseover=

"document.images.example.src=image02.src"

onmouseout= "document.images.example.src=image01.src">

<img src=" images/ordown.jpg " name="example“ border=“0”>

</a></body>

</html>

Page 16: javascript examples

16

Rollover Example<html><head><script type="text/javascript"><!--image01= new Image(36,46)image01.src="images/ordown.jpg"image02= new Image(46,52)image02.src="images/orup.jpg"

function roll_out(){document.images.example.src=image02.src}

function roll_in(){document.images.example.src=image01.src}//--></script></head>

<body><h1>Figure Out Why Only The First Button Words</h1><a href="" onmouseover="roll_out()" onmouseout= "roll_in()" ><img src=" images/ordown.jpg " name="example" border="0"></a><hr><a href="" onmouseover="roll_out()" onmouseout= "roll_in()" ><img src=" images/ordown.jpg " name="example" border="0"></a><hr><a href="" onmouseover="roll_out()" onmouseout= "roll_in()" ><img src=" images/ordown.jpg " name="example" border="0"></a></body>

</html>

Page 17: javascript examples

17

Form Validation

Page 18: javascript examples

18

What is Form Validation?

Form validation is the process of checking that a form has been filled in correctly before it is processed.

For example, to check that user has filled in a valid email address.

Page 19: javascript examples

19

Form Validation Using form elements in Javascript

Capturing form entries in variables

Testing string attributes such as length and character sets

See: http://java

scriptkit.com/script/cutindex13.shtml

Page 20: javascript examples

20

Form Elements

<form onSubmit = “ …”><input type = … name = … /><select><option name=… value = ….> Text 2 </option><option name = .. value = ….>Text 2 </option>

</select></form>

Page 21: javascript examples

21

String Length Check

function checkUsername (strng) { var error = ""; if ((strng.length < 4) || (strng.length > 10)) { error = "The username is the wrong

length.\n"; }

}

Page 22: javascript examples

22

Using Regular Expressions for Checking Existence of Character