Web2 Lec3
Web2 Lec3
Mohammed 1
Outlines
• JavaScript
• Jquery
2
What is JavaScript?
• HTML and CSS concentrate on a static rendering of
a page; things do not change on the page over time,
or because of events.
4
JavaScript
• In HTML, JavaScript code is inserted
between <script> and </script> tags.
5
Benefits
• JavaScript Can Change HTML Content and
css
– One of many JavaScript HTML methods is
getElementById().
• document.getElementById("demo").innerHTML = "
Hello JavaScript";
• document.getElementById("demo").style.fontSize = "
35px";
• Hide HTML Elements
– document.getElementById("demo").style.display
= "none";
7
Disadvantages of client side
scripting
• Code is usually visible.
10
External JavaScript
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>
– Examples :
– Var x = 5; //for older versions of JS to 2015
– let y = 6; //the declaration after 2015
– const body_temp=37; // for not change values
–x = x + 5
the equal sign (=) is an "assignment" operator,
not an "equal to" operator
12
JavaScript naming and Comments
• A JavaScript name must begin with:
– A letter (A-Z or a-z)
– A dollar sign ($)
– Or an underscore (_)
• Comments :
– Code after double slashes // or between /* and */ is treated as a
comment.
13
JavaScript Operators
let x = 5;
• JavaScript Arithmetic Operators: let y = 2;
– + Addition // let z = x + y;
– - Subtraction
– * Multiplication //let z = x * y;
– ** Exponentiation (ES2016)
– / Division
– % Modulus (Division Remainder)
– ++ Increment
– -- Decrement
14
JavaScript Array
• An array is a special variable, which can hold more
than one value.
• Syntax
– const array_name = [item1, item2, ...];
– By using new Array()
• const cars = new Array("Volvo", "BMW");
const cars = [
"Volvo",
"BMW"
];
15
Accessing Array Elements
• const cars = [ "Volvo", "BMW"];
• cars[0] = "Opel";
• To get all elements of the array :
– document.getElementById("demo").
innerHTML = cars;
16
JavaScript Array Methods
• toString()
– converts an array to a string of (comma separated)
array values.
– document.getElementById("demo").innerHTML =
array.toString();
17
JavaScript Functions
• in javascript functions are created with the
keyword function as show nbelow:
function funname( )
{
Your code here.......
}
18
Calling functions example
<HTML> <HTML>
<HEAD> <HEAD>
<TITLE> Function direct call</TITLE> <TITLE> Function dynamically</TITLE>
<script language="JavaScript"> <script language="JavaScript">
function add(x,y) function add( )
{ {
x=20
z=x+y
y=30
return z
z=x+y
} document.write("addition is :"+z);
</script> }
</HEAD> </script>
<BODY> </HEAD>
<script> <BODY> to call function:
var r=add(30,60) <input type="button" value="click hare"
document.write("addition is :"+r); onclick="add( )">
</script> </script>
</BODY> </BODY>
</HTML> </HTML>
19
JavaScript Conditions
• If and else if
if (condition1) {
code to be executed if condition1 is true
}
else if (condition2) {
code to be executed if condition2 is true
}
else {
code to be executed if neither condition1 nor
condition2 is true
}
20
JavaScript Loops
• JavaScript supports different kinds of loops:
– for - loops through a block of code a number of times
– for/in- loops through the properties of an object
– while - loops through a block of code while a specified
condition is true
– do/while - also loops through a block of code while a
specified condition is true
21
JavaScript Objects
• JavaScript has several built-in objects, like
String, Date, Array, and more.
22
Accessing Object Properties
• This example uses the length property of
the String object to find the length of a
string:
– var message="Hello World!";
– var x=message.length;
23
Accessing Objects Methods
• Methods are the actions that can be performed
on objects.
24
JS Objects
25
JS Objects Constructor
26
Document Object in JS
• JavaScript Can Change HTML Content:
– document.getElementById("demo").innerHTML
= "Hello JavaScript";
28
Built-in Objects
• JS String
• JS Date
• JS Array
• JS Boolean
• JS Math
• JS RegExp
29
Math Object
• The Math object allows you to perform
mathematical tasks.
30
Math Object
• Math Object Methods:
– sqrt(x)
– abs(x)
– ceil(x)
– floor(x)
– round(x)
– sin(x)
– max(x,y,z,...,n)
– min(x,y,z,...,n)
– pow(x,y)
31
Events handling in JavaScript
• Event handlers are attributes that force an
element to "listen" for a specific event to occur.
32
Events
• Attribute The event occurs when…
• onclick mouse click an object
• ondblclick. mouse double clicks
• onmouseover a mouse cursor on touch here
• onmousedown a mouse button ispressed
• onmousemove the mouse is moved
• on mouse out the mouse is moved out anelement
• onmouseup a mouse button isreleased
• onkeydown a keyboard key ispressed
• onkeypress a keyboard key is pressed or held down
• onkeyup a keyboard key isreleased
• onfocus an elements getfocus
• onchange the content of a fieldchange
• onsubmit the submit button isclicked
33
Example
<HTML>
<HEAD>
<script language="JavaScript">
function myf( )
{
document.write("Hai Mohammed")
}
</script>
</HEAD>
<BODY>
to execute script code:
<input type="button" value="click me" onclick="myf( )">
To execute script code:
<input type="button" value="touch me" onmouseover="myf( )">
</BODY>
</HTML>
34
JavaScript frameworks
• Framework
– A JavaScript framework is a collection of pre-
written code built to support applications.
• Common frameworks:
– Angular
– React
– Vue
– Ember
– Next
– Node
35
A JS library for developing web pages
JQUERY
36
jQuery
• jQuery is a JavaScript Library.
• jQuery is easy to learn.
• The purpose of jQuery is to make it much
easier to use JavaScript on your website.
• jQuery is a lightweight, "write less, do
more", JavaScript library.
• jQuery will run exactly the same in all
major browsers.
37
Why jQuery?
• jQuery is probably the most popular, and also
the most extendable JavaScript library .
38
Adding jQuery to Your Web Pages
40
jQuery Syntax
$(document). $(function(){
ready(function(){ // jQuery methods go
// jQuery methods go here...
here... });
});
42
jQuery Selectors
Element ID selector
$(document). $(document).
.
ready(function(){ ready(function(){
$("button").click(function(){ $("button").
$("p").hide(); click(function(){
});}); $("#test").hide();
});});
Class selector Element Class selector
$(document).
ready(function(){ $("p.intro")
$("button").
click(function(){ Selects all <p>
$(".test").hide(); elements with
});}); class="intro" 43
jQuery Event Methods
• All the different visitors actions that a web page can respond to
are called events.
• Syntax :
$("p").event(function(){
// action goes here!!
}); $("p").on({
• Common events: mouseenter: function(){
• click() $(this).css("background-color"
• dblclick()
• mouseenter()
, "lightgray"); },
• mouseleave() mouseleave: function(){
• hover() $(this).css("background-color"
• focus()
• On() , "lightblue");
},});
44
jQuery Effects
• Syntax :
– $(selector).hide(speed,callback);
• Speed values :"slow", "fast", or milliseconds.
• Callback :function to be executed after the hide() or show() method
completes
• Common jQuery Effects :
– hide() $("p").click(function(){
– show() $(this).hide();
– toggle()
– fadeIn()
});
– fadeOut()
– fadeToggle()
– fadeTo() :opacity value between 0 and 1
– slideDown()
– slideUp() $("input").focus(function(){
– slideToggle()
– focus()
$(this).css("background-color", "
#cccccc");}); 45
Downloading
• https://jquery.com/
– Ex: Version 3.6.0
47