L5 Javascript
L5 Javascript
JAVASCRIPT
1
SCRIPTING LANGUAGES
3
JAVASCRIPT
JavaScript is a scripting language produced by Netscape for use
within HTML Web pages.
JavaScript is loosely based on Java and it is built into all the
major modern browsers.
An object-oriented computer programming language commonly
used to create interactive effects within web browsers.
The most common use of JavaScript is to add client-side
behavior to HTML pages, also known as Dynamic HTML 4
(DHTML).
JAVASCRIPT
Scripts are embedded in or included from HTML pages and
interact with the Document Object Model (DOM) of the
page.
One difference between a scripting language and a
programming language is that scripting languages do not
require the compilation step and are rather interpreted.
o For example, normally, a C program needs to be compiled before
running whereas normally, a scripting language like JavaScript or
PHP need not be compiled. 5
4. Perl
o Old UNIX language
o Found on all Windows and Linux servers
o Can handle text manipulation tasks
o Excellent web scripting language
o Server side execution 9
DIFFERENT SCRIPTING LANGUAGES
5. PHP (Hypertext Pre-Processor)
o Especially good at connecting to MySQL
o Very popular language
o Runs on UNIX and Windows
o HTML-embedded scripting language
o Syntax looks like C, JAVA, and PERL
o Generates Dynamic content and good User Interface
o Server side execution 10
DIFFERENT SCRIPTING LANGUAGES
6. JSP (Java Server Pages)
o JavaServer Pages (JSP) is a technology for developing web pages
that support dynamic content which helps developers insert java
code in HTML pages by making use of special JSP tags, most of
which start with <% and end with %>.
o Developed by Sun Microsystems
o Uses Java
o similar to PHP and ASP, but it uses the Java programming
language
o Allows you to easily create web content that has both static and 11
dynamic components.
DIFFERENT SCRIPTING LANGUAGES
7. CGI (Common Gateway Interface)
o It offers a standard protocol for web servers to interface with
executable programs running on a server that generate web pages
dynamically.
o Such programs are known as CGI scripts or simply as CGIs; though usually
written in a scripting language, they can be written in any programming
language.
o Needs to launch separate instance of application for each web request.
o CGI is often used to process input information from the user and produce the
12
appropriate output.
DIFFERENT SCRIPTING LANGUAGES
8. VBScript
oMicrosoft’s scripting language
oClient side Scripting language
oVery easy to learn
oIncludes the functionality of Visual Basic 13
DIFFERENT SCRIPTING LANGUAGES
9. JavaScript
o Client-side Scripting language
o Easy to use programming language
o Enhance dynamics and interactive features of a web page
o Allows one to perform calculation, write interactive games,
add special effects, customize graphic selections, create
security passwords 14
QUESTION
16
COMMON USES OF JAVASCRIPT
Used to add multimedia elements
With JavaScript you can show, hide, change, resize images, and create
image rollovers.
You can create scrolling text across the status bar.
Create pages dynamically.
Based on the user's choices, the date, or other external data, JavaScript
can produce pages that are customized to the user.
Interact with the user.
It can do some processing of forms and can validate user input when the
17
</script>
JAVASCRIPT SYNTAX:
The script tag takes two important attributes:
Language: This attribute specifies what scripting language
you are using. Typically, its value will be JavaScript. Although
recent versions of HTML (and XHTML, its successor) have
phased out the use of this attribute.
Type: This attribute is what is now recommended to indicate
the scripting language in use and its value should be set to 22
"text/javascript".
JAVASCRIPT SYNTAX:
So your JavaScript segment will look like:
OR:
</SCRIPT>
IMPLEMENTING JAVASCRIPT
SourceFile.js
document.writeln("Hello World");
28
1. EXTERNAL FILE
The biggest advantage to having an external Javascript file is
that once the file has been loaded, the script will hang around
the browser's cache which means if the Javascript is loaded on
one page then it's almost a sure thing that the next page on the
site the user visits will be able to load the file from the
browser's cache instead of having to reload it over the
Internet .
This is an incredibly fast and speedy process.
29
2. IN-LINE JAVASCRIPT
30
EXTERNAL FILE VS IN-LINE JAVASCRIPT
31
EXAMPLE 2: IN-LINE JAVASCRIPT
<html>
<body>
<script language="javascript" type="text/javascript">
</script>
</body> 32
</html>
COMMENTS IN JAVASCRIPT
JavaScript supports both C-style and C++-style comments, Thus:
Any text between a // and the end of a line is treated as a comment.
Any text between the characters /* and */ is treated as a comment.
This may span multiple lines.
JavaScript also recognizes the HTML comment opening sequence
<!--. JavaScript treats this as a single-line comment, just as it does
the // comment.
The HTML comment closing sequence --> is not recognized by 33
34
1. BROWSER OUTPUT
For Javascript there are three ways to communicate
with the user:
a) document.writeln (string) command:
o This can be used while the page is being constructed.
o After the page has finished loading a new
document.writeln (string) command will delete the page in
most browsers, so we use this only while the page is 35
loading.
EXERCISE1:
36
EXERCISE1: OUTPUT
37
1. BROWSER OUTPUT
b) Output(alert)
o The second method is to use a browser alert box.
o While these are incredibly useful for debugging (and
learning the language), they are a horrible way to
communicate with the user because they stop your scripts
from running until the user clicks the OK button. 39
EXERCISE 2:
<p>
This is a test paragraph!!!
</p>
<script type='text/javascript'>
alert('Hello World!');
</script>
<p>
This is test paragraph number 2 40
</p>
EXERCISE 2:
41
1. BROWSER OUTPUT
c) Output (getElementById)
o This is the most powerful and the most complex method.
o Everything on a web page resides in a box.
o You can give each and every box in HTML a unique
identifier (an ID), and Javascript can find boxes you have
labeled and let you manipulate them. 42
EXERCISE 3:
43
1. BROWSER OUTPUT
The left part of the statement says on this web page
(document) find a block we've named "feedback"
(getElementById ('feedback') ), and change its HTML
(innerHTML) to be 'Hello World!'.
We can change the contents of 'feedback' at any time, even
after the page has finished loading (which document.writeln
can't do), and without annoying the user with a bunch of 44
46
COMMON HTML EVENTS
Events listed on w3schools page are related to the following objects
Mouse Print
Keyboard Media
Frame/Object Animation
Form Transition
Drag Server-sent
Clipboard Misc
Touch 47
http://www.w3schools.com/jsref/dom_obj_event.asp
EXAMPLE: MOUSE EVENTS
48
2. INPUT
a) Click input
49
2. INPUT
a) Output
o Written text changes after clicking.
50
2. INPUT
b) User Input
o You can add an onClick event to almost any HTML element, but
sometimes you need to be able to ask for input from the user and
process it.
o For that you'll need a basic form element and a button.
o Here we create an input field and give it a name of userInput.
o Then we create a HTML button with an onClick event that will call
51
o These are all standard HTML form elements but they're not
bound by a <form> tag since we're not going to be submitting
this information to a server.
o Instead, when the user clicks the submit button, the onClick
event will call the userSubmit() function…
52
EXERCISE 5:
53
EXERCISE 5: OUTPUT
54
ONKEYUP EVENT
56
EXERCISE 5B: OUTPUT
57
CHANGE THE COLOR OF TEXT WHEN A MOUSE GOES
OVER IT
58
CHANGE THE COLOR OF TEXT WHEN A MOUSE GOES
OVER IT
59
SEARCH FOR AND STATE THE POSITION OF AN
ELEMENT WITHIN A GIVEN SEARCH STRING
<script language="javascript1.1">
var supersetString="
abctopghijklmnopqrstuvwxyzabctopghijklm ";
var subsetString="top";
var lPosition=supersetString.indexOf(subsetString);
alert(lPosition);
60
</script>
ACCESS ELEMENT AT POSITION 5
var str = "abctopghijklmnopqrstuvwxyzabctopghijklm"
alert(str.charAt(5))
61
LOWERCASE THE FIRST CHARACTER
Alert("ABCTOPGHIJKLMNOPQRSTUVWXYZABCTOPGHIJKLM".
charAt(0).toLowerCase() )
62
CHANGE THE BACKGROUND COLOR OF A WEBPAGE WITH ONMOUSEOVER
AND MOUSEOUT EVENTS
63
OUTPUT
64
JAVASCRIPT: LANGUAGE SYNTAX
65
JAVASCRIPT: LANGUAGE SYNTAX
\' Single quote. Used to represent a single quote character in a string. For
example,
window.alert( '\'in quotes\'' ); 67
*
**
***
**** 68
*****
ARITHMETIC OPERATORS
JavaScript Arithmetic Algebraic JavaScript
operation operator expression expression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y or x / y
Remainder % r mod s r % s
6
EXERCISE 9:
70
RELATIONAL (INEQUALITY AND EQUALITY)
OPERATORS
Standard algebraic JavaScript equality Sample Meaning of
equality operator or or relational JavaScript JavaScript
relational operator operator condition condition
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or
equal to y
<= x <= y x is less than or equal
to y
Note the difference between a relational equality operator “==“ with an
71
EXERCISE 10:
72
VARIABLES AND DATA TYPES
Type of a variable is dynamic: depends on the type of data it contains
JavaScript has six data types:
o Number
o String
o Boolean (values true and false)
o Object
o Null (only value of this type is null)
o Undefined (value of newly created variable) 73
75
JAVASCRIPT STATEMENTS
JavaScript keyword
76
JAVASCRIPT OPERATORS
Operators are used to create compound expressions from
simpler expressions
Operators can be classified according to the number of
operands involved:
o Unary: one operand (e.g., typeof i)
• Prefix or postfix (e.g., ++i or i++ )
o Binary: two operands (e.g., x + y) 77
80
JAVASCRIPT FUNCTIONS
Function call syntax
81
JAVASCRIPT FUNCTIONS
82
QUESTION B
Create a web-page that contains 3 text-boxes and a button.
The user enters values in 2 textboxes, and when he clicks the
command button the result is displayed in the 3rd text box.
83