SlideShare une entreprise Scribd logo
1  sur  51
UNIT-5
Introduction to Java Script
(Client side programming)
Introduction to Java Script
Browsers have limited functionality
Text, images, tables, frames
JavaScript allows for interactivity
Browser/page manipulation
Reacting to user actions
A type of programming language
Easy to learn
Developed by Netscape
JavaScript Allows Interactivity
•Improve appearance
•Especially graphics
•Visual feedback
•Site navigation
•Perform calculations
•Validation of input
•Other technologies
Embedded within HTML page
View source
Executes on client
Fast, no connection needed once loaded
Simple programming statements combined with HTML
tags
Interpreted (not compiled)
 No special tools required
How Does It Work?
•Special syntax to learn
•Learn the basics and then use other
people's (lots of free sites)
•Write it in a text editor, view results in
browser
•You need to revise your HTML
•You need patience and good eyesight
Learning JavaScript
Why Study JavaScript?
JavaScript is one of the 3 languages all web
developers must learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language="JavaScript">
document.write('This is my first 
JavaScript Page');
</script>
</body>
</html>
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language="JavaScript">
document.write(‘<h2>This is my first 
JavaScript Page</h2>');
</script>
</body>
</html>
HTML written
inside JavaScript
JavaScript Statements
HTML written
inside JavaScript
<html>
<head><title>My Page</title></head>
<body>
<p>
<a href="myfile.html">My Page</a>
<br />
<a href="myfile.html"
onMouseover="window.alert('Hello');">
My Page</A>
</p>
</body>
</html>
JavaScript outputs
JavaScript can "display" data in different ways:
Writing into an HTML element, using innerHTML.
Writing into the HTML output using document.write().
Writing into an alert box, using window.alert().
Writing into the browser console, using console.log().
Using innerHTML
To access an HTML element, JavaScript can use the document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML content:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Using document.write()
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Using document.write()
Example
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<button type="button" onclick="document.write(5 + 6)">Try it</button>
</body>
</html>
Using window.alert()
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Using console.log()
Example
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript Where To
•In HTML, JavaScript code must be inserted between <script> and
</script> tags.
•Scripts can be placed in the <body>, or in the <head> section of an
HTML page, or in both.
•External JavaScript External file: myScript.js
Ex:
function myFunction()
{
document.getElementById("demo").innerHTML = "Paragraph
changed.";
}
<script src="myScript.js"></script>
JavaScript Variables
JavaScript variables are containers for storing data values.
Example
var x = 5;
var y = 6;
var z = x + y;
The general rules for constructing names for variables (unique
identifiers) are:
Names can contain letters, digits, underscores, and dollar signs.
Names must begin with a letter
Names can also begin with $ and _ (but we will not use it in this tutorial)
Names are case sensitive (y and Y are different variables)
Reserved words (like JavaScript keywords) cannot be used as names
Declaring (Creating) JavaScript Variables
Creating a variable in JavaScript is called "declaring" a variable.
You declare a JavaScript variable with the var keyword:
var carName;
After the declaration, the variable has no value. (Technically it has
the value of undefined)
To assign a value to the variable, use the equal sign:
carName = "Volvo";
Declaring (Creating) JavaScript Variables
Example
<p id="demo"></p>
<script>
var carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
</script>
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task.
A JavaScript function is executed when "something" invokes it (calls it).
JavaScript Function Syntax:
A JavaScript function is defined with the function keyword, followed by a name,
followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same
rules as variables).
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
function name(parameter1, parameter2, parameter3)
{
code to be executed
}
JavaScript Functions
Function parameters are listed inside the parentheses () in the function
definition.
Function arguments are the values received by the function when it is invoked.
Inside the function, the arguments (the parameters) behave as local variables.
Function Invocation:
The code inside the function will execute when "something" invokes (calls) the
function:
•When an event occurs (when a user clicks a button)
•When it is invoked (called) from JavaScript code
•Automatically (self invoked)
JavaScript Functions
Function Return
•When JavaScript reaches a return statement, the function will stop executing.
•If the function was invoked from a statement, JavaScript will "return" to execute
the code after the invoking statement.
•Functions often compute a return value.
•The return value is "returned" back to the "caller":
Example
Calculate the product of two numbers, and return the result:
var x = myFunction(4, 3); // Function is called, return value will end up in x
function myFunction(a, b)
{
return a * b; // Function returns the product of a and b
}
JavaScript Functions
Function Return
•When JavaScript reaches a return statement, the function will stop executing.
•If the function was invoked from a statement, JavaScript will "return" to execute
the code after the invoking statement.
•Functions often compute a return value.
•The return value is "returned" back to the "caller":
Example
Calculate the product of two numbers, and return the result:
var x = myFunction(4, 3); // Function is called, return value will end up in x
function myFunction(a, b)
{
return a * b; // Function returns the product of a and b
}
JavaScript Functions
The () Operator Invokes the Function
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Accessing a function without () will return the function definition instead of the function result:</p>
<p id="demo"></p>
<script>
function toCelsius(f)
{
return (5/9) * (f-32);
}
document.getElementById("demo").innerHTML = toCelsius;
</script>
</body>
</html>
JavaScript Functions
Output:
Accessing a function without () will return the function definition instead of the
function result:
function toCelsius(f) { return (5/9) * (f-32); }
JavaScript Scope
Scope determines the accessibility (visibility) of variables.
In JavaScript there are two types of scope:
•Local scope
•Global scope
JavaScript has function scope: Each function creates a new
scope.
JavaScript Scope(cont..)
Local JavaScript Variables:
Variables declared within a JavaScript function,
become LOCAL to the function.
Local variables have local scope: They can only be
accessed within the function.
Example
// code here can not use carName
function myFunction() {
var carName = "Volvo";
// code here can use carName
}
Local Variables: Example Program
<!DOCTYPE html>
<html>
<body>
<p>The local variable carName cannot be accessed from code outside the function:</p>
<p id="demo"></p>
<script>
myFunction();
document.getElementById("demo").innerHTML =
"The type of carName is " + typeof carName;
function myFunction() {
var carName = "Volvo";
}
</script>
</body>
</html>
JavaScript Scope(cont..)
Global JavaScript Variables
•A variable declared outside a function, becomes GLOBAL.
•A global variable has global scope: All scripts and
functions on a web page can access it.
Example
var carName = " Volvo";
// code here can use carName
function myFunction()
{
// code here can use carName
}
Example code:<!DOCTYPE html>
<html>
<body>
<p>A GLOBAL variable can be accessed from any script or function.</p>
<p id="demo"></p>
<script>
var carName = "Volvo";
myFunction();
function myFunction()
{
document.getElementById("demo").innerHTML =
"I can display " + carName;
}
</script>
</body>
Automatically Global
If you assign a value to a variable that has not been declared, it will
automatically become a GLOBALvariable.
This code example will declare a global variable carName, even if
the value is assigned inside a function.
Example
myFunction();
// code here can use carName
function myFunction() {
carName = "Volvo";
}
JavaScript Events
HTML events are "things" that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can "react" on
these events.
HTML Events
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
•An HTML web page has finished loading
•An HTML input field was changed
•An HTML button was clicked
JavaScript Events
HTML allows event handler attributes, with JavaScript code, to be
added to HTML elements.
With single quotes:
<element event='some JavaScript'>
With double quotes:
<element event="some JavaScript">
In the following example, an onclick attribute (with code), is added
to a button element:
Example
<button onclick="document.getElementById('demo').innerHTML =
Date()">The time is?</button>
JavaScript Events
Common HTML Events
Here is a list of some common HTML events:
EventDescription
•Onchange An HTML element has been changed
•Onclick The user clicks an HTML element
•Onmouseover The user moves the mouse over an HTML element
•Onmouseout The user moves the mouse away from an HTML
element
• onkeydown The user pushes a keyboard key
•Onload The browser has finished loading the page
JavaScript Events
Event handlers can be used to handle, and verify, user input, user
actions, and browser actions:
•Things that should be done every time a page loads
•Things that should be done when the page is closed
•Action that should be performed when a user clicks a button
•Content that should be verified when a user inputs data..etc.
Many different methods can be used to let JavaScript work with
events:
•HTML event attributes can execute JavaScript code directly
•HTML event attributes can call JavaScript functions
•You can assign your own event handler functions to HTML elements
•You can prevent events from being sent or being handled..etc..
JavaScript Events
•Onclick The user clicks an HTML element
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('demo').innerHTML=Date()">The
time is?</button>
<p id="demo"></p>
</body>
</html>
JavaScript Events
•Onclick The user clicks an HTML element
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('demo').innerHTML=Date()">The
time is?</button>
<p id="demo"></p>
</body>
</html>
JavaScript Events
Onkeydown and onkeyup The user clicks an HTML element
(example 1)
<!DOCTYPE html>
<html>
<body>
<p>Press and hold down a key inside the text field to set a red background color. Release the key to
set a green background color.</p>
<input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()">
<script>
function keydownFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
function keyupFunction() {
document.getElementById("demo").style.backgroundColor = "green";
}
</script>
JavaScript Events
Onkeydown and onkeyup The user clicks an HTML element
(example 1)
<!DOCTYPE html>
<html>
<body>
<p>This example uses the addEventListener() method to attach a "keydown" event to an input element.</p>
<p>Press a key inside the text field to set a red background color.</p>
<input type="text" id="demo">
<script>
document.getElementById("demo").addEventListener("keydown", myFunction);
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
</body>
</html>
JavaScript Events
onmousedown Event
The onmousedown event occurs when a user presses a mouse
button over an element.
Tip: The order of events related to the onmousedown event (for
the left/middle mouse button):
•onmousedown
•onmouseup
•onclick
JavaScript Events
onmousedown Event
<!DOCTYPE html>
<html>
<body>
<p>This example uses the addEventListener() method to attach a "mousedown" and "mouseup" event to a p element.</p>
<p id="demo">Click me.</p>
<script>
document.getElementById("demo").addEventListener("mousedown", mouseDown);
document.getElementById("demo").addEventListener("mouseup", mouseUp);
function mouseDown() {
document.getElementById("demo").innerHTML = "The mouse button is held down.";
}
function mouseUp() {
document.getElementById("demo").innerHTML = "You released the mouse button.";
}
</script>
</body>
</html>
JavaScript Form Validation
HTML form validation can be done by JavaScript.
If a form field (fname) is empty, this function alerts a message, and
returns false, to prevent the form from being submitted:
JavaScript Example
function validateForm()
{
var x = document.forms["myForm"]["fname"].value;
if (x == "")
{
alert("Name must be filled out");
return false;
}
}
Form Validation program
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="/action_page.php"
onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
Form Validation program
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Can Validate Input</h2>
<p>Please input a number between 1 and 10:</p>
<input id="numb">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script>
function myFunction() {
var x, text;
// Get the value of the input field with id="numb"
x = document.getElementById("numb").value;
// If x is Not a Number or less than one or greater than 10
if (isNaN(x) || x < 1 || x > 10) {
text = "Input not valid";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
Automatic HTML Form Validation
HTML form validation can be performed automatically by the browser:
If a form field (fname) is empty, the required attribute prevents this form from
being submitted:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
<p>If you click submit, without filling out the text field,
your browser will display an error message.</p>
</body>
</html>
Data Validation
Data validation is the process of ensuring that user input is clean, correct, and
useful.
Typical validation tasks are:
•has the user filled in all required fields?
•has the user entered a valid date?
•has the user entered text in a numeric field?
Most often, the purpose of data validation is to ensure correct user input.
Validation can be defined by many different methods, and deployed in many
different ways.
Server side validation is performed by a web server, after input has been sent to
the server.
Client side validation is performed by a web browser, before input is sent to a
web server.
HTML Constraint Validation
HTML5 introduced a new HTML validation concept called constraint validation.
HTML constraint validation is based on:
•Constraint validation HTML Input Attributes
•Constraint validation CSS Pseudo Selectors
•Constraint validation DOM Properties and Methods
Constraint Validation HTML Input Attributes
Attribute Description
disabled Specifies that the input element should be disabled
max Specifies the maximum value of an input element
min Specifies the minimum value of an input element
pattern Specifies the value pattern of an input element
required Specifies that the input field requires an element
type Specifies the type of an input element
Example 1
<!DOCTYPE html>
<html>
<body>
<h2>The readonly Attribute</h2>
<p>The readonly attribute specifies that the input field is read only (cannot be
changed):</p>
<form action="">
First name:<br>
<input type="text" name="firstname" value ="John" readonly>
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<body>
<h2>The disabled Attribute</h2>
<p>The disabled attribute specifies that the input field is disabled:</p>
<form action="">
First name:<br>
<input type="text" name="firstname" value ="John" disabled>
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
</body>
</html>

Contenu connexe

Tendances

Tendances (20)

Javascript session 01 - Introduction to Javascript
Javascript session 01 - Introduction to JavascriptJavascript session 01 - Introduction to Javascript
Javascript session 01 - Introduction to Javascript
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
PHP - Introduction to PHP Date and Time Functions
PHP -  Introduction to  PHP Date and Time FunctionsPHP -  Introduction to  PHP Date and Time Functions
PHP - Introduction to PHP Date and Time Functions
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
19servlets
19servlets19servlets
19servlets
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATIONPHP BASIC PRESENTATION
PHP BASIC PRESENTATION
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Lecture 5 javascript
Lecture 5 javascriptLecture 5 javascript
Lecture 5 javascript
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
PHP
PHPPHP
PHP
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015
 
Java script
Java scriptJava script
Java script
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
tut0000021-hevery
tut0000021-heverytut0000021-hevery
tut0000021-hevery
 

Similaire à Introduction to JavaScript for Beginners

Similaire à Introduction to JavaScript for Beginners (20)

JavaScriptL18 [Autosaved].pptx
JavaScriptL18 [Autosaved].pptxJavaScriptL18 [Autosaved].pptx
JavaScriptL18 [Autosaved].pptx
 
Java Script
Java ScriptJava Script
Java Script
 
JAVASCRIPT 1.pptx.pptx
JAVASCRIPT 1.pptx.pptxJAVASCRIPT 1.pptx.pptx
JAVASCRIPT 1.pptx.pptx
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
jQuery
jQueryjQuery
jQuery
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
 
27javascript
27javascript27javascript
27javascript
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]
 
Java script
Java scriptJava script
Java script
 
Javascript
JavascriptJavascript
Javascript
 
Java script tutorial
Java script tutorialJava script tutorial
Java script tutorial
 
Javascript
JavascriptJavascript
Javascript
 
Function calling in js
Function calling in jsFunction calling in js
Function calling in js
 
Iwt note(module 2)
Iwt note(module 2)Iwt note(module 2)
Iwt note(module 2)
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
FYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III JavascriptFYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III Javascript
 

Dernier

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Dernier (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 

Introduction to JavaScript for Beginners

  • 1.
  • 2. UNIT-5 Introduction to Java Script (Client side programming)
  • 3. Introduction to Java Script Browsers have limited functionality Text, images, tables, frames JavaScript allows for interactivity Browser/page manipulation Reacting to user actions A type of programming language Easy to learn Developed by Netscape
  • 4. JavaScript Allows Interactivity •Improve appearance •Especially graphics •Visual feedback •Site navigation •Perform calculations •Validation of input •Other technologies
  • 5. Embedded within HTML page View source Executes on client Fast, no connection needed once loaded Simple programming statements combined with HTML tags Interpreted (not compiled)  No special tools required How Does It Work?
  • 6. •Special syntax to learn •Learn the basics and then use other people's (lots of free sites) •Write it in a text editor, view results in browser •You need to revise your HTML •You need patience and good eyesight Learning JavaScript
  • 7. Why Study JavaScript? JavaScript is one of the 3 languages all web developers must learn: 1. HTML to define the content of web pages 2. CSS to specify the layout of web pages 3. JavaScript to program the behavior of web pages
  • 8. JavaScript Statements <html> <head><title>My Page</title></head> <body> <script language="JavaScript"> document.write('This is my first  JavaScript Page'); </script> </body> </html>
  • 9. JavaScript Statements <html> <head><title>My Page</title></head> <body> <script language="JavaScript"> document.write(‘<h2>This is my first  JavaScript Page</h2>'); </script> </body> </html> HTML written inside JavaScript
  • 10. JavaScript Statements HTML written inside JavaScript <html> <head><title>My Page</title></head> <body> <p> <a href="myfile.html">My Page</a> <br /> <a href="myfile.html" onMouseover="window.alert('Hello');"> My Page</A> </p> </body> </html>
  • 11. JavaScript outputs JavaScript can "display" data in different ways: Writing into an HTML element, using innerHTML. Writing into the HTML output using document.write(). Writing into an alert box, using window.alert(). Writing into the browser console, using console.log().
  • 12. Using innerHTML To access an HTML element, JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content: Example <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My First Paragraph</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 + 6; </script> </body> </html>
  • 13. Using document.write() Example <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> document.write(5 + 6); </script> </body> </html>
  • 14. Using document.write() Example <!DOCTYPE html> <html> <body> <h2>My First Web Page</h2> <p>My first paragraph.</p> <button type="button" onclick="document.write(5 + 6)">Try it</button> </body> </html>
  • 15. Using window.alert() Example <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> window.alert(5 + 6); </script> </body> </html>
  • 17. JavaScript Where To •In HTML, JavaScript code must be inserted between <script> and </script> tags. •Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both. •External JavaScript External file: myScript.js Ex: function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } <script src="myScript.js"></script>
  • 18. JavaScript Variables JavaScript variables are containers for storing data values. Example var x = 5; var y = 6; var z = x + y; The general rules for constructing names for variables (unique identifiers) are: Names can contain letters, digits, underscores, and dollar signs. Names must begin with a letter Names can also begin with $ and _ (but we will not use it in this tutorial) Names are case sensitive (y and Y are different variables) Reserved words (like JavaScript keywords) cannot be used as names
  • 19. Declaring (Creating) JavaScript Variables Creating a variable in JavaScript is called "declaring" a variable. You declare a JavaScript variable with the var keyword: var carName; After the declaration, the variable has no value. (Technically it has the value of undefined) To assign a value to the variable, use the equal sign: carName = "Volvo";
  • 20. Declaring (Creating) JavaScript Variables Example <p id="demo"></p> <script> var carName = "Volvo"; document.getElementById("demo").innerHTML = carName; </script>
  • 21. JavaScript Functions A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). JavaScript Function Syntax: A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...) The code to be executed, by the function, is placed inside curly brackets: {} function name(parameter1, parameter2, parameter3) { code to be executed }
  • 22. JavaScript Functions Function parameters are listed inside the parentheses () in the function definition. Function arguments are the values received by the function when it is invoked. Inside the function, the arguments (the parameters) behave as local variables. Function Invocation: The code inside the function will execute when "something" invokes (calls) the function: •When an event occurs (when a user clicks a button) •When it is invoked (called) from JavaScript code •Automatically (self invoked)
  • 23. JavaScript Functions Function Return •When JavaScript reaches a return statement, the function will stop executing. •If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. •Functions often compute a return value. •The return value is "returned" back to the "caller": Example Calculate the product of two numbers, and return the result: var x = myFunction(4, 3); // Function is called, return value will end up in x function myFunction(a, b) { return a * b; // Function returns the product of a and b }
  • 24. JavaScript Functions Function Return •When JavaScript reaches a return statement, the function will stop executing. •If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. •Functions often compute a return value. •The return value is "returned" back to the "caller": Example Calculate the product of two numbers, and return the result: var x = myFunction(4, 3); // Function is called, return value will end up in x function myFunction(a, b) { return a * b; // Function returns the product of a and b }
  • 25. JavaScript Functions The () Operator Invokes the Function <!DOCTYPE html> <html> <body> <h2>JavaScript Functions</h2> <p>Accessing a function without () will return the function definition instead of the function result:</p> <p id="demo"></p> <script> function toCelsius(f) { return (5/9) * (f-32); } document.getElementById("demo").innerHTML = toCelsius; </script> </body> </html>
  • 26. JavaScript Functions Output: Accessing a function without () will return the function definition instead of the function result: function toCelsius(f) { return (5/9) * (f-32); }
  • 27. JavaScript Scope Scope determines the accessibility (visibility) of variables. In JavaScript there are two types of scope: •Local scope •Global scope JavaScript has function scope: Each function creates a new scope.
  • 28. JavaScript Scope(cont..) Local JavaScript Variables: Variables declared within a JavaScript function, become LOCAL to the function. Local variables have local scope: They can only be accessed within the function. Example // code here can not use carName function myFunction() { var carName = "Volvo"; // code here can use carName }
  • 29. Local Variables: Example Program <!DOCTYPE html> <html> <body> <p>The local variable carName cannot be accessed from code outside the function:</p> <p id="demo"></p> <script> myFunction(); document.getElementById("demo").innerHTML = "The type of carName is " + typeof carName; function myFunction() { var carName = "Volvo"; } </script> </body> </html>
  • 30. JavaScript Scope(cont..) Global JavaScript Variables •A variable declared outside a function, becomes GLOBAL. •A global variable has global scope: All scripts and functions on a web page can access it. Example var carName = " Volvo"; // code here can use carName function myFunction() { // code here can use carName }
  • 31. Example code:<!DOCTYPE html> <html> <body> <p>A GLOBAL variable can be accessed from any script or function.</p> <p id="demo"></p> <script> var carName = "Volvo"; myFunction(); function myFunction() { document.getElementById("demo").innerHTML = "I can display " + carName; } </script> </body>
  • 32. Automatically Global If you assign a value to a variable that has not been declared, it will automatically become a GLOBALvariable. This code example will declare a global variable carName, even if the value is assigned inside a function. Example myFunction(); // code here can use carName function myFunction() { carName = "Volvo"; }
  • 33. JavaScript Events HTML events are "things" that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can "react" on these events. HTML Events An HTML event can be something the browser does, or something a user does. Here are some examples of HTML events: •An HTML web page has finished loading •An HTML input field was changed •An HTML button was clicked
  • 34. JavaScript Events HTML allows event handler attributes, with JavaScript code, to be added to HTML elements. With single quotes: <element event='some JavaScript'> With double quotes: <element event="some JavaScript"> In the following example, an onclick attribute (with code), is added to a button element: Example <button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>
  • 35. JavaScript Events Common HTML Events Here is a list of some common HTML events: EventDescription •Onchange An HTML element has been changed •Onclick The user clicks an HTML element •Onmouseover The user moves the mouse over an HTML element •Onmouseout The user moves the mouse away from an HTML element • onkeydown The user pushes a keyboard key •Onload The browser has finished loading the page
  • 36. JavaScript Events Event handlers can be used to handle, and verify, user input, user actions, and browser actions: •Things that should be done every time a page loads •Things that should be done when the page is closed •Action that should be performed when a user clicks a button •Content that should be verified when a user inputs data..etc. Many different methods can be used to let JavaScript work with events: •HTML event attributes can execute JavaScript code directly •HTML event attributes can call JavaScript functions •You can assign your own event handler functions to HTML elements •You can prevent events from being sent or being handled..etc..
  • 37. JavaScript Events •Onclick The user clicks an HTML element <!DOCTYPE html> <html> <body> <button onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button> <p id="demo"></p> </body> </html>
  • 38. JavaScript Events •Onclick The user clicks an HTML element <!DOCTYPE html> <html> <body> <button onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button> <p id="demo"></p> </body> </html>
  • 39. JavaScript Events Onkeydown and onkeyup The user clicks an HTML element (example 1) <!DOCTYPE html> <html> <body> <p>Press and hold down a key inside the text field to set a red background color. Release the key to set a green background color.</p> <input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()"> <script> function keydownFunction() { document.getElementById("demo").style.backgroundColor = "red"; } function keyupFunction() { document.getElementById("demo").style.backgroundColor = "green"; } </script>
  • 40. JavaScript Events Onkeydown and onkeyup The user clicks an HTML element (example 1) <!DOCTYPE html> <html> <body> <p>This example uses the addEventListener() method to attach a "keydown" event to an input element.</p> <p>Press a key inside the text field to set a red background color.</p> <input type="text" id="demo"> <script> document.getElementById("demo").addEventListener("keydown", myFunction); function myFunction() { document.getElementById("demo").style.backgroundColor = "red"; } </script> </body> </html>
  • 41. JavaScript Events onmousedown Event The onmousedown event occurs when a user presses a mouse button over an element. Tip: The order of events related to the onmousedown event (for the left/middle mouse button): •onmousedown •onmouseup •onclick
  • 42. JavaScript Events onmousedown Event <!DOCTYPE html> <html> <body> <p>This example uses the addEventListener() method to attach a "mousedown" and "mouseup" event to a p element.</p> <p id="demo">Click me.</p> <script> document.getElementById("demo").addEventListener("mousedown", mouseDown); document.getElementById("demo").addEventListener("mouseup", mouseUp); function mouseDown() { document.getElementById("demo").innerHTML = "The mouse button is held down."; } function mouseUp() { document.getElementById("demo").innerHTML = "You released the mouse button."; } </script> </body> </html>
  • 43. JavaScript Form Validation HTML form validation can be done by JavaScript. If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted: JavaScript Example function validateForm() { var x = document.forms["myForm"]["fname"].value; if (x == "") { alert("Name must be filled out"); return false; } }
  • 44. Form Validation program <!DOCTYPE html> <html> <head> <script> function validateForm() { var x = document.forms["myForm"]["fname"].value; if (x == "") { alert("Name must be filled out"); return false; } } </script> </head> <body> <form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post"> Name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> </body>
  • 45. Form Validation program <!DOCTYPE html> <html> <body> <h2>JavaScript Can Validate Input</h2> <p>Please input a number between 1 and 10:</p> <input id="numb"> <button type="button" onclick="myFunction()">Submit</button> <p id="demo"></p> <script> function myFunction() { var x, text; // Get the value of the input field with id="numb" x = document.getElementById("numb").value; // If x is Not a Number or less than one or greater than 10 if (isNaN(x) || x < 1 || x > 10) { text = "Input not valid"; } else { text = "Input OK"; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>
  • 46. Automatic HTML Form Validation HTML form validation can be performed automatically by the browser: If a form field (fname) is empty, the required attribute prevents this form from being submitted: <!DOCTYPE html> <html> <body> <form action="/action_page.php" method="post"> <input type="text" name="fname" required> <input type="submit" value="Submit"> </form> <p>If you click submit, without filling out the text field, your browser will display an error message.</p> </body> </html>
  • 47. Data Validation Data validation is the process of ensuring that user input is clean, correct, and useful. Typical validation tasks are: •has the user filled in all required fields? •has the user entered a valid date? •has the user entered text in a numeric field? Most often, the purpose of data validation is to ensure correct user input. Validation can be defined by many different methods, and deployed in many different ways. Server side validation is performed by a web server, after input has been sent to the server. Client side validation is performed by a web browser, before input is sent to a web server.
  • 48. HTML Constraint Validation HTML5 introduced a new HTML validation concept called constraint validation. HTML constraint validation is based on: •Constraint validation HTML Input Attributes •Constraint validation CSS Pseudo Selectors •Constraint validation DOM Properties and Methods
  • 49. Constraint Validation HTML Input Attributes Attribute Description disabled Specifies that the input element should be disabled max Specifies the maximum value of an input element min Specifies the minimum value of an input element pattern Specifies the value pattern of an input element required Specifies that the input field requires an element type Specifies the type of an input element
  • 50. Example 1 <!DOCTYPE html> <html> <body> <h2>The readonly Attribute</h2> <p>The readonly attribute specifies that the input field is read only (cannot be changed):</p> <form action=""> First name:<br> <input type="text" name="firstname" value ="John" readonly> <br> Last name:<br> <input type="text" name="lastname"> </form> </body> </html>
  • 51. Example 2 <!DOCTYPE html> <html> <body> <h2>The disabled Attribute</h2> <p>The disabled attribute specifies that the input field is disabled:</p> <form action=""> First name:<br> <input type="text" name="firstname" value ="John" disabled> <br> Last name:<br> <input type="text" name="lastname"> </form> </body> </html>