This document provides an introduction to JavaScript and its uses for web programming. It explains that JavaScript is a client-side scripting language that allows web pages to become interactive. Some key points covered include:
- JavaScript can change HTML content, styles, validate data, and make calculations.
- Functions are blocks of code that perform tasks when invoked by events or called in code.
- Events like clicks or keyboard presses trigger JavaScript code.
- The DOM (Document Object Model) represents an HTML document that JavaScript can access and modify.
- Forms and user input can be accessed and processed using the DOM.
- Programming flow can be controlled with conditional and loop statements.
-
What is JavaScript?
ď§JavaScriptis used to program the behaviour of web pages
(performing dynamic tasks).
ď§Javascript are scripts (code) that is executed on the clientâs browser
instead of the web-server (Client-side scripts).
4.
Why we needclient side programming
ď§The userâs actions will result in an immediate response because they
don't require a trip to the server.
ď§ Allows the creation of faster and more responsive web applications.
ď§ Make web pages more interactive
ď§Fewer resources are used and needed on the web-server.
5.
Characteristics of JavaScript
Object-based
â˘Doesn't support all the features of OOPs like Polymorphism and Inheritance
Event-Driven
⢠Much of the JavaScript code you write will be in response to events generated by the
user or the system.
Browser-Dependent
⢠JavaScript depends on the Web browser to support it. If the browser does not support
it, your code will be ignored. Even worse, the JavaScript code itself may be displayed
as text on your page.
6.
Characteristics of JavaScript(cont.)
Interpreted language
⢠JavaScript is interpreted at runtime by the browser before it is executed.
JavaScript is not compiled into an actual program like an .EXE file but
remains part of the HTML document to which it is attached.
Dynamic
⢠You can declare variables of a specific type, but you do not need to
Case sensitive
7.
What Javascript cando?
ď§Javascript can change HTML Content
ď§Javascript can change HTML Attributes
ď§Javascript can change HTML Styles (CSS)
ď§Javascript can Validate Data
ď§Javascript can Make Calculations
8.
Embedding JavaScript inHTML
1. Anywhere in the html file between <script></script> tags.
2. As the value of the event handler attributes.
9.
Embedding JavaScript inHTML
3. In an external file and refer to it using the SRC attribute.
Note:
Keeping all code in one place, is always a good habit.
10.
JavaScript Display Possibilities
JavaScriptcan "display" data in different ways:
ď§Writing into an alert box, using window.alert(), window.prompt(),
window.confirm().
ď§Writing into an HTML element, using innerHTML.
Events
ď§Event handlers arecreated as attributes added to the HTML tags in
which the event is triggered.
ď§An Event handler adopts the event name and appends the word
âonâ in front of it.
ď§Thus the âclickâ event becomes the onclick event handler.
14.
Mouse Events
Event handlerDescription
onmousedown when pressing any of the mouse buttons.
onmousemove when the user moves the mouse pointer within an
element.
onmouseout when moving the mouse pointer out of an element.
onmouseup when the user releases any mouse button pressed
onmouseover when the user moves the mouse pointer over an
element.
onclick when clicking the left mouse button on an element.
ondblclick when Double-clicking the left mouse button on an
element.
ondragstart When the user has begun to select an element
15.
Keyboard Events
Event handlerDescription
onkeydown When User holds down a key
onkeypress When User presses a key
onkeyup When User releases the pressed a key
16.
JavaScript Functions
ď§A JavaScriptfunction is a block of code designed to perform a
particular task.
ď§A function is executed when "something" invokes it (calls it).
ď§ When an event occurs (when a user clicks a button)
ď§ When it is invoked (called) from JavaScript code
ď§ Automatically (self invoked)
Function Return
ď§When JavaScriptreaches a return statement, the function will stop executing
and returns some value to the invoker.
ď§If the function was invoked from a statement, JavaScript will "return" to
execute the code after the invoking statement.
JavaScript Built-in functions
NameDescription Example
isFinite(num)
(used to test number)
returns true if the
number is finite, else
false
document.write(isFinite(2.2345))
//returns true
document.write(isFinite(âHelloâ))
//returns false
isNaN(val)
(used to test value)
validate the argument
for a number and
returns true if the
given value is not a
number else returns
false.
document.write(isNaN(0/0))
//returns true
document.write(isNaN("348"))
//returns false
eval(expression)
evaluates an
expression and
returns the result.
f=999; w=777;
document.write(eval(f + w));
// returns 1776
23.
JavaScript Built-in functions
NameDescription Example
escape(string)
method converts the special
characters like space, colon
etc. of the given string in to
escape sequences.
escape("test val");
//test%20val
unescape(string)
function replaces the escape
sequences with original
values.
e.g. %20 is the escape
sequence for space " ".
unescape("test%20val");
//test val
24.
JavaScript Primitive Valuetypes
Value Example
Number Any numeric value (e.g., 3, 5.3, or 45e8)
String
Any string of alphanumeric characters
(e.g., "Hello, World!", "555-1212" or
"KA12V2B334")
Boolean True or False values only
25.
JavaScript Special Values
ValueExample
Null
Eg: var x = null;
A special keyword for the null value
(no value or empty variable)
** x has the type âobjectâ
Undefined
Eg: var x;
A special keyword means that a value
hasn't even been assigned yet.
** x has the type âundefinedâ
26.
JavaScript Variables
ď§Variables arecontainers that hold values.
ď§Variables are untyped
ď§The initial value for any variable is undefined.
var num; //num = undefined
ď§ While it is not technically necessary, variable declarations should begin
with the keyword var.
27.
JavaScript Variables Scope
ď§Global Scope
ď§ Local Scope
<script>
x=1
var y=2
function MyFunction()
{
var z
z=3
// the rest of the code
}
</script>
Global scope
Local scope
28.
Controlling Program Flow
ď§Programflow is normally linear
ď§Control Statements can change the program flow
1. Conditional Statements
a. if âŚ.else
b. switch/case
2. Loop Statements
a. for
b. for..in loops through the properties of an object
c. while
d. doâŚwhile
29.
HTML DOM (DocumentObject Model)
The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.
30.
Finding HTML Elements
MethodDescription
document.getElementById() Find an element by element id
document.getElementsByTagName() Find elements by tag name
document.getElementsByClassName() Find elements by class name
31.
Changing HTML elements
MethodDescription
element.innerHTML=
Change the inner HTML of an
element
element.attribute=
Change the attribute of an HTML
element
element.setAttribute(attribute,value)
Change the attribute of an HTML
element
element.style.property= Change the style of an HTML element
32.
Adding and deletingelements
Method Description
document.createElement(...) Create an HTML element
document.removeChild(...) Remove an HTML element
document.appendChild(...) Add an HTML element
document.replaceChild(...) Replace an HTML element
document.write(text) Write into the HTML output stream
HTML Forms andJavaScript
ď§HTML <form> elements receive input
ď§JavaScript is very good at processing user input in the web browser
ď§Forms and form elements have unique names
ď§Each unique element can be identified
ď§Uses JavaScript Document Object Model (DOM)
Using Form data
ď§Toaccess the values of the form element use the following syntax:
document.formname.elementname.value
ď§Example
document.addressform.yourname.value
document.addressform.phone.value
document.addressform.email.value
Implement the following
ď§Implementthe following cart:
ď§When the user clicks Add, the
item name, price and a Remove
button are added to the cart.
ď§When the user clicks Remove, the
corresponding item is removed
from the cart.
ď§There should be a Total Price that is
affected when an item is added or
removed.
ď§Due date: 14-10-2017