SlideShare a Scribd company logo
Javascript Examples
Discuss regular expressions in Java course, show
             their use in Javascript



             J M Githeko


                                                   1
Agenda

 Dynamic Effects: Button roll-overs
 Form Validation




                                       2
Button Rollovers

 Image access using DOM
 Image properties
 Image stored on object variables
  (preloading)
 Changing image properties using
  event handlers


                                     3
Step 1: Creating the Basic Page
<html>
  <head>
  <title>Image Rollover</title>
  </head>
 <body bgcolor="white">
 <img src="screw.jpg">
 </body>
 </html>

                                  4
Step 2: Naming the Image

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




                                    5
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
                                        6
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                                     7

 We can change the src property
Step 5: Creating the Functions

<script style = “text/javascript">

     function roll_this()
 {
     document.images.pic1.src="capnut.jpg";
     }

     </script>

                                              8
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

                                               9
Rollover for a Single Button
<a href=""
  onMouseOver="document.images.pic1.src='capnut.jpg'"
  onMouseOut="document.images.pic1.src='screw.jpg'">
  </a>




                                                 10
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”;

                                      11
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>

                                    12
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]                                  13
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>                         14
</head>
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”>
                                        15
</a>
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>



                                                              16
Form Validation




                  17
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.



                                         18
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
                                             19
Form Elements

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


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

}                                            21
Using Regular Expressions for
Checking Existence of Character




                                  22

More Related Content

What's hot (20)

PPTX
course js day 3
Georgyi Grigoryev
 
PDF
React
중운 박
 
PPTX
The AngularJS way
Boyan Mihaylov
 
PPTX
Dundee University HackU 2013 - Mojito
smartads
 
PDF
AngularJS: an introduction
Luigi De Russis
 
PDF
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe
 
PDF
AngularJS Framework
Barcamp Saigon
 
PPTX
MVC and Razor - Doc. v1.2
Naji El Kotob
 
PDF
Reactive Type-safe WebComponents
Martin Hochel
 
PDF
JQuery UI
Gary Yeh
 
PPT
Jquery ui
adm_exoplatform
 
PDF
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Jeado Ko
 
PPTX
Mvc & java script
Eyal Vardi
 
RTF
Java script frame window
H K
 
PPTX
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
PDF
Odoo Experience 2018 - Develop an App with the Odoo Framework
ElínAnna Jónasdóttir
 
PPTX
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
PDF
Introduction to AngularJS
Marco Vito Moscaritolo
 
PDF
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
ElínAnna Jónasdóttir
 
PPTX
Ext JS Introduction
Anand Dayalan
 
course js day 3
Georgyi Grigoryev
 
React
중운 박
 
The AngularJS way
Boyan Mihaylov
 
Dundee University HackU 2013 - Mojito
smartads
 
AngularJS: an introduction
Luigi De Russis
 
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe
 
AngularJS Framework
Barcamp Saigon
 
MVC and Razor - Doc. v1.2
Naji El Kotob
 
Reactive Type-safe WebComponents
Martin Hochel
 
JQuery UI
Gary Yeh
 
Jquery ui
adm_exoplatform
 
Angular를 활용한 웹 프론트단 개발과 2.0에서 달라진점
Jeado Ko
 
Mvc & java script
Eyal Vardi
 
Java script frame window
H K
 
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
Odoo Experience 2018 - Develop an App with the Odoo Framework
ElínAnna Jónasdóttir
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Introduction to AngularJS
Marco Vito Moscaritolo
 
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
ElínAnna Jónasdóttir
 
Ext JS Introduction
Anand Dayalan
 

Viewers also liked (10)

KEY
Events
Josh Guo
 
PPTX
Javascript Tlabs
msneha
 
PPT
Java Script - Module I
Mustafa Qamar-ud-Din
 
PPT
Javascript Intro 01
vikram singh
 
PPTX
Unit 1-introduction to scripts
sana mateen
 
PPT
Scripting languages
teach4uin
 
PPTX
Scripting languages
Diane Phillips Krebs
 
PPTX
Web servers
Kuldeep Kulkarni
 
PPT
Web Servers (ppt)
webhostingguy
 
PPT
Lect 1. introduction to programming languages
Varun Garg
 
Events
Josh Guo
 
Javascript Tlabs
msneha
 
Java Script - Module I
Mustafa Qamar-ud-Din
 
Javascript Intro 01
vikram singh
 
Unit 1-introduction to scripts
sana mateen
 
Scripting languages
teach4uin
 
Scripting languages
Diane Phillips Krebs
 
Web servers
Kuldeep Kulkarni
 
Web Servers (ppt)
webhostingguy
 
Lect 1. introduction to programming languages
Varun Garg
 
Ad

Similar to javascript examples (20)

PDF
POLITEKNIK MALAYSIA
Aiman Hud
 
PPS
CS101- Introduction to Computing- Lecture 41
Bilal Ahmed
 
PDF
JavaScript
tutorialsruby
 
PDF
JavaScript
tutorialsruby
 
PPTX
MTA animations graphics_accessing data
Dhairya Joshi
 
KEY
ARTDM 170, Week 3: Rollovers
Gilbert Guerrero
 
KEY
ARTDM 170, Week 3: Rollovers
Gilbert Guerrero
 
PPTX
Geekspeak: Javascript
lisakennelly
 
KEY
ARTDM 170 Week 3: Rollovers
Gilbert Guerrero
 
PDF
Java script
Ramesh Kumar
 
PPT
Easy javascript
Bui Kiet
 
PDF
Presenter manual RIA technology (specially for summer interns)
XPERT INFOTECH
 
PPT
Learn javascript easy steps
prince Loffar
 
PDF
www.webre24h.com - [O`reilly] javascript cookbook - [powers]
webre24h
 
PPTX
01 Introduction - JavaScript Development
Tommy Vercety
 
PDF
Progressive Enhancement with JavaScript and Ajax
Christian Heilmann
 
PPTX
Presentation Tier optimizations
Anup Hariharan Nair
 
PDF
jQuery Introduction
Arwid Bancewicz
 
PDF
lecture5
tutorialsruby
 
PDF
lecture5
tutorialsruby
 
POLITEKNIK MALAYSIA
Aiman Hud
 
CS101- Introduction to Computing- Lecture 41
Bilal Ahmed
 
JavaScript
tutorialsruby
 
JavaScript
tutorialsruby
 
MTA animations graphics_accessing data
Dhairya Joshi
 
ARTDM 170, Week 3: Rollovers
Gilbert Guerrero
 
ARTDM 170, Week 3: Rollovers
Gilbert Guerrero
 
Geekspeak: Javascript
lisakennelly
 
ARTDM 170 Week 3: Rollovers
Gilbert Guerrero
 
Java script
Ramesh Kumar
 
Easy javascript
Bui Kiet
 
Presenter manual RIA technology (specially for summer interns)
XPERT INFOTECH
 
Learn javascript easy steps
prince Loffar
 
www.webre24h.com - [O`reilly] javascript cookbook - [powers]
webre24h
 
01 Introduction - JavaScript Development
Tommy Vercety
 
Progressive Enhancement with JavaScript and Ajax
Christian Heilmann
 
Presentation Tier optimizations
Anup Hariharan Nair
 
jQuery Introduction
Arwid Bancewicz
 
lecture5
tutorialsruby
 
lecture5
tutorialsruby
 
Ad

More from Egerton University (7)

PPTX
COMP340 TOPIC 4 THREE.JS.pptx
Egerton University
 
PPTX
Android ui with xml
Egerton University
 
PPTX
Event handler example
Egerton University
 
PPTX
Android programming basics
Egerton University
 
PPTX
Php basics
Egerton University
 
PPTX
Website management
Egerton University
 
PPTX
My sql command line client
Egerton University
 
COMP340 TOPIC 4 THREE.JS.pptx
Egerton University
 
Android ui with xml
Egerton University
 
Event handler example
Egerton University
 
Android programming basics
Egerton University
 
Php basics
Egerton University
 
Website management
Egerton University
 
My sql command line client
Egerton University
 

Recently uploaded (20)

PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Dimensions of Societal Planning in Commonism
StefanMz
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
community health nursing question paper 2.pdf
Prince kumar
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 

javascript examples

  • 1. Javascript Examples Discuss regular expressions in Java course, show their use in Javascript J M Githeko 1
  • 2. Agenda  Dynamic Effects: Button roll-overs  Form Validation 2
  • 3. Button Rollovers  Image access using DOM  Image properties  Image stored on object variables (preloading)  Changing image properties using event handlers 3
  • 4. Step 1: Creating the Basic Page <html> <head> <title>Image Rollover</title> </head> <body bgcolor="white"> <img src="screw.jpg"> </body> </html> 4
  • 5. Step 2: Naming the Image <img src="screw.jpg" name="pic1"> 5
  • 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 6
  • 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 7  We can change the src property
  • 8. Step 5: Creating the Functions <script style = “text/javascript"> function roll_this() { document.images.pic1.src="capnut.jpg"; } </script> 8
  • 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 9
  • 10. Rollover for a Single Button <a href="" onMouseOver="document.images.pic1.src='capnut.jpg'" onMouseOut="document.images.pic1.src='screw.jpg'"> </a> 10
  • 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”; 11
  • 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> 12
  • 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] 13
  • 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> 14 </head>
  • 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”> 15 </a>
  • 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> 16
  • 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. 18
  • 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 19
  • 20. Form Elements <form onSubmit = “ …”> <input type = … name = … /> <select> <option name=… value = ….> Text 2 </option> <option name = .. value = ….>Text 2 </option> </select> </form> 20
  • 21. String Length Check function checkUsername (strng) { var error = ""; if ((strng.length < 4) || (strng.length > 10)) { error = "The username is the wrong length.n"; } } 21
  • 22. Using Regular Expressions for Checking Existence of Character 22