SlideShare a Scribd company logo
1 of 40
JavaScript Basics
Presented by : Manisha Chugh
Batch Code: B-14/PHP/04-01
Registration id: R13010624
What is JavaScript?
• JavaScript is a scripting language designed for Web
pages.
• It enhances Web pages with dynamic and interactive
features.
• It enables shopping carts, form validation,
calculations, special graphic and text effects, image
swapping and more.
©Copyright 2014 by Manisha All Rights
Reserved.
Methods of Using
JavaScript
1. External - JavaScript can reside in a separate page.
2. Internal - JavaScript can be embedded in HTML
documents -- in the <head>, in the <body>, or in both.
3. Inline - JavaScript object attributes can be placed in
HTML element tags.
©Copyright 2014 by Manisha All Rights
Reserved.
External JavaScript
• Linking can be advantageous if many pages use the
same script.
• Use the source element to link to the script file.
<script src="myjavascript.js”
type="text/javascript">
</script>
©Copyright 2014 by Manisha All Rights
Reserved.
Internal JavaScript
• When specifying a script only the tags <script>
and </script> are essential, but complete
specification is recommended:
<script language="javascript”
type="text/javascript">
<!-- Begin hiding
window.location=”index.html"
// End hiding script-->
</script>
©Copyright 2014 by Manisha All Rights
Reserved.
Using Comment Tags
• HTML comment tags should bracket any script.
• The <!-- script here --> tags hide scripts in
HTML and prevent scripts from displaying in browsers
that do not interpret JavaScript.
• Double slashes // are the signal characters for a
JavaScript single-line comment.
©Copyright 2014 by Manisha All Rights
Reserved.
Inline JavaScript
• Event handlers like onLoad are a perfect example of
an easy to add tag script.
e.g.
<body onLoad="alert('WELCOME')">
©Copyright 2014 by Manisha All Rights
Reserved.
JavaScript Terminology
• Objects,
• Properties,
• Functions,
• Methods,
• Events,
• Listeners,
• Variables.
©Copyright 2014 by Manisha All Rights
Reserved.
Objects
• Objects refers to windows, documents, images,
tables, forms, buttons or links, etc.
• Objects should be named.
• Objects have properties that act as modifiers.
©Copyright 2014 by Manisha All Rights
Reserved.
Properties
• Properties are object attributes.
• Object properties are defined by using the object's
name, a period, and the property name.
• e.g., background color is expressed by:
document.bgcolor.
Here, document is the object.
bgcolor is the property.
©Copyright 2014 by Manisha All Rights
Reserved.
Functions
• A group of statements that is put together once and
then can be used repeatedly on a Web page.
• JavaScript has built-in functions, and you can write
your own.
©Copyright 2014 by Manisha All Rights
Reserved.
Format of a function
definition
function function-name( parameter-list )
{
declarations and statements
}
Here,
• Function name is any valid identifier.
• Parameter list contains names of variables that will receive
arguments.
• Declarations and statements are function body (“block” of
code)
©Copyright 2014 by Manisha All Rights
Reserved.
Methods
• Methods are functions.
• They are unusual in the sense that they are stored as
properties of objects.
• e.g., document.write(”Hello World")
Here, document is the object.
write is the method.
©Copyright 2014 by Manisha All Rights
Reserved.
Events
• Events are visitor’s and browser’s activities.
• There can be two types of events:
• User-Initiated Events
• Browser-Initiated Events
©Copyright 2014 by Manisha All Rights
Reserved.
Browser-Initiated Events
• load
• unload
• error
• abort
©Copyright 2014 by Manisha All Rights
Reserved.
User-Initiated Events
• click
• dblclick
• keydown
• keyup
• keypress
• mouseover
• mouseout
• mousedown
• mouseup
• mousemove
• change
• resize
• scroll
• select
• blur
• focus
• submit
©Copyright 2014 by Manisha All Rights
Reserved.
Listeners
• Listeners capture and actually respond to those
events. Thus, they are also known as Event
Handlers.
• The system sends events to the listener program and
the program responds to them as they arrive.
©Copyright 2014 by Manisha All Rights
Reserved.
Listeners (Cont.)
• Events handlers are placed in the BODY part of a Web page as
attributes in HTML tags.
e.g.
<input type=button name=btn1 value=“Click
Me” onClick=“alert(‘button was
clicked’);”>
• Alternatively, events can be captured in the HTML code, and then
directed to a JavaScript function for an appropriate response.
©Copyright 2014 by Manisha All Rights
Reserved.
Listeners (Cont.)
e.g.
<head>
<script language= "JavaScript"
type="text/javascript">
function mymessage()
{
alert(“Hello User!");
}
</script>
</head>
<body onload="mymessage()">
</body>
©Copyright 2014 by Manisha All Rights
Reserved.
Various Event Handlers
• The following event handlers are available in
JavaScript:
• onAbort
• onBlur
• onChange
• onClick
• onDragDrop
• onError
• onFocus
• onKeyDown
• onKeyPress
• onKeyUp
• onSubmit
• onMouseDown
• onMouseMove
• onMouseOut
• onMouseOver
• onMouseUp
• onResize
• onSelect
• onSubmit
• onLoad
• onUnload
©Copyright 2014 by Manisha All Rights
Reserved.
Various Event Handlers
(Cont.)
• onAbort
An abort event occurs when a user aborts the loading of an image
(for example by clicking a link or clicking the Stop button).
• onBlur
A blur event occurs when object on a form loses focus.
• onChange
A change event occurs when any object (select, text, or textarea)
loses focus and its value has been modified.
• onClick
A click event occurs when an object on a form is clicked.
©Copyright 2014 by Manisha All Rights
Reserved.
Various Event Handlers
(Cont.)
• onDragDrop
A drapDrop event occurs when a user drops an object onto the browser
window, such as dropping a file.
• onError
An error event occurs when the loading of a document or image causes an
error.
• onFocus
A focus event occurs when a field receives input focus by tabbing with the
keyboard or clicking with the mouse.
• onKeyDown, onKeyPress, onKeyUp
A keyDown, keyPress, or keyUp event occurs when a user depresses a key,
presses or holds down a key, or releases a key, respectively.
©Copyright 2014 by Manisha All Rights
Reserved.
Various Event Handlers
(Cont.)
• onMouseDown, onMouseMove, onMouseOut,
onMouseOver, and onMouseUp
A MouseDown, MouseMove, MouseOut, MouseOver, or MouseUp
event occurs when a user depresses a mouse button, moves a cursor,
moves a cursor out of the object, moves a cursor over the object,
releases a mouse button, respectively.
• onResize
A Resize event occurs when a user or script resizes a window.
©Copyright 2014 by Manisha All Rights
Reserved.
Various Event Handlers
(Cont.)
• onSelect
A select event occurs when a user selects some of the text within a text or
textarea field.
• onSubmit
A submit event occurs when a user submits a form.
• onLoad
A load event occurs after a web page is loaded.
• onUnload
An unload event occurs when you exit a web page.
©Copyright 2014 by Manisha All Rights
Reserved.
Event Handlers and the
“this” Keyword
• ‘This’ keyword is used for self-reference of object when an
event handler calls a JavaScript function.
e.g.
<input type="button" value="press me" onclick
= "callfunc(this);">
// Here ‘this’ keyword refers to the Button
object.
©Copyright 2014 by Manisha All Rights
Reserved.
Variables
• Variables contain values and use the equal sign to specify
their value.
• Variables are created by declaration using the var
command with or without an initial value state.
• e.g. var month;
• e.g. var month = April;
©Copyright 2014 by Manisha All Rights
Reserved.
Dialog Boxes
• Dialog boxes can be used to –
• inform the user of errors or events
• confirm actions initiated by the user
• ask some information from the user
©Copyright 2014 by Manisha All Rights
Reserved.
Alert Dialog Box
• Alert Dialog Box informs the user about errors or events.
User can just read the message and press OK.
e.g.
alert(‘Hello! Welcome to my website.');
©Copyright 2014 by Manisha All Rights
Reserved.
Confirm Dialog Box
• Confirm Dialog Box confirms actions initiated by the
user. User can confirm or Cancel the action.
e.g.
confirm('Are you sure you want to exit?');
©Copyright 2014 by Manisha All Rights
Reserved.
Prompt Dialog Box
• Prompt Dialog Box is used to get information from the
user.
e.g.
prompt('Enter your name:');
©Copyright 2014 by Manisha All Rights
Reserved.
Accessing HTML Elements
In JavaScript
• In JavaScript, you can access specific HTML elements by
using a pre-defined variable called document and requesting
the element by ID.
• Accessing an HTML element by ID, general syntax:
document.getElementById("<ID>")
e.g.
document.getElementById(“d1”).innerHTML=“Hello
World!”;
<div id=“d1”></div>
©Copyright 2014 by Manisha All Rights
Reserved.
Accessing HTML Elements
In JavaScript (Cont.)
• Once you have access to a particular HTML element, you can
access all its attributes using the "dot" operator.
• Accessing an element's attribute, general syntax:
document.getElementById("<ID>").<attribute>
• Accessing form element, general syntax:
document.formname.elementname.value
©Copyright 2014 by Manisha All Rights
Reserved.
Example
<form name="alertform">
Enter your name:
<input type="text" name="yourname">
<input type="button" value= "Go"
onClick="window.alert('Hello ' +
document.alertform.yourname.value);">
</form>
©Copyright 2014 by Manisha All Rights
Reserved.
Accessing CSS Properties
• Accessing CSS properties, general syntax:
document.getElementById("<ID>").style.<property>
• Property names in JavaScript are identical to those in CSS, but
with namesLikeThis instead of names-like-this
e.g.
backgroundColor instead of background-color
• Example:
document.getElementById("name").style.backgroundColor =
"yellow";
©Copyright 2014 by Manisha All Rights
Reserved.
Form Validations
• JavaScript is very good at processing and validating user input
in the web browser.
• Requirements to perform validations:
• Form’s name must be specified.
• Object’s name must be specified.
• Input must be of Submit type.
• Validations can be applied in two ways:
1. Traditional iterative programming methodologies
2. Pattern matching (via regular expressions)
©Copyright 2014 by Manisha All Rights
Reserved.
Traditional iterative
programming methodologies
• Write a small program (function) that iterates
through the user input and validates the data type
and returns true or false depending on result.
• This can be relatively slow, especially if form has
many fields to be validated.
©Copyright 2014 by Manisha All Rights
Reserved.
Example
function validate()
{
if(document.login.uname.value==“”)
{
alert(“Please enter username”);
document.login.uname.focus();
return false;
}
return true;
}
<form name=“login” action=“” onsubmit=“validate()”>
<input type=“text” name=“uname”>
©Copyright 2014 by Manisha All Rights
Reserved.
Pattern matching
• Create a regular expression pattern for the data type
and let the system validate the user input via its
pattern matching capabilities.
• Pattern matching is very quick (even for long
forms).
©Copyright 2014 by Manisha All Rights
Reserved.
HTML5 Validation
• required
<input type=“text” name=“uname” required/>
• pattern= ‘a regular expression’ like text, search,
url, telephone, email, and password.
<input type=“email” name=“email”/>
• min= max = step= can be used with <input type=range>
<input type=“range” max=“30”>
©Copyright 2014 by Manisha All Rights
Reserved.
Thank You!

More Related Content

What's hot (20)

Css Ppt
Css PptCss Ppt
Css Ppt
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
HTML5
HTML5HTML5
HTML5
 
SQL
SQLSQL
SQL
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
CSS Positioning and Features of CSS3
CSS Positioning and Features of CSS3CSS Positioning and Features of CSS3
CSS Positioning and Features of CSS3
 
Programming fundamentals using c++ question paper 2014 tutorialsduniya
Programming fundamentals using c++  question paper 2014   tutorialsduniyaProgramming fundamentals using c++  question paper 2014   tutorialsduniya
Programming fundamentals using c++ question paper 2014 tutorialsduniya
 
Dtd
DtdDtd
Dtd
 
Css
CssCss
Css
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
 
Xhtml
XhtmlXhtml
Xhtml
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Html heading
Html headingHtml heading
Html heading
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
String operation
String operationString operation
String operation
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 

Viewers also liked

JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPTGo4Guru
 
01 Introduction - JavaScript Development
01 Introduction - JavaScript Development01 Introduction - JavaScript Development
01 Introduction - JavaScript DevelopmentTommy Vercety
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneAn Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneEvent Handler
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascripttonyh1
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript IntroductionDesignveloper
 
Front-end development introduction (JavaScript). Part 2
Front-end development introduction (JavaScript). Part 2Front-end development introduction (JavaScript). Part 2
Front-end development introduction (JavaScript). Part 2Oleksii Prohonnyi
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascriptKumar
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScriptCodemotion
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptBryan Basham
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
JavaScript code academy - introduction
JavaScript code academy - introductionJavaScript code academy - introduction
JavaScript code academy - introductionJaroslav Kubíček
 

Viewers also liked (17)

JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
01 Introduction - JavaScript Development
01 Introduction - JavaScript Development01 Introduction - JavaScript Development
01 Introduction - JavaScript Development
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneAn Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
Front-end development introduction (JavaScript). Part 2
Front-end development introduction (JavaScript). Part 2Front-end development introduction (JavaScript). Part 2
Front-end development introduction (JavaScript). Part 2
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascript
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Java script
Java scriptJava script
Java script
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScript
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
JavaScript code academy - introduction
JavaScript code academy - introductionJavaScript code academy - introduction
JavaScript code academy - introduction
 

Similar to Learn Javascript Basics

Similar to Learn Javascript Basics (20)

Javascript 2
Javascript 2Javascript 2
Javascript 2
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
 
JavaScript
JavaScriptJavaScript
JavaScript
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdf
 
JavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdfJavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdf
 
Java script
Java scriptJava script
Java script
 
Java script
Java scriptJava script
Java script
 
JAVASCRIPT 1.pptx.pptx
JAVASCRIPT 1.pptx.pptxJAVASCRIPT 1.pptx.pptx
JAVASCRIPT 1.pptx.pptx
 
CHAPTER 3 JS (1).pptx
CHAPTER 3  JS (1).pptxCHAPTER 3  JS (1).pptx
CHAPTER 3 JS (1).pptx
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
Unit 4(it workshop)
Unit 4(it workshop)Unit 4(it workshop)
Unit 4(it workshop)
 
WP - Unit I.ppt
WP - Unit I.pptWP - Unit I.ppt
WP - Unit I.ppt
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
 
Javascript
JavascriptJavascript
Javascript
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 
Building interactive app
Building interactive appBuilding interactive app
Building interactive app
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & Implementation
 
javascript 1
javascript 1javascript 1
javascript 1
 
Introduction to HTML language Web design.pptx
Introduction to HTML language Web design.pptxIntroduction to HTML language Web design.pptx
Introduction to HTML language Web design.pptx
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

Learn Javascript Basics

  • 1. JavaScript Basics Presented by : Manisha Chugh Batch Code: B-14/PHP/04-01 Registration id: R13010624
  • 2. What is JavaScript? • JavaScript is a scripting language designed for Web pages. • It enhances Web pages with dynamic and interactive features. • It enables shopping carts, form validation, calculations, special graphic and text effects, image swapping and more. ©Copyright 2014 by Manisha All Rights Reserved.
  • 3. Methods of Using JavaScript 1. External - JavaScript can reside in a separate page. 2. Internal - JavaScript can be embedded in HTML documents -- in the <head>, in the <body>, or in both. 3. Inline - JavaScript object attributes can be placed in HTML element tags. ©Copyright 2014 by Manisha All Rights Reserved.
  • 4. External JavaScript • Linking can be advantageous if many pages use the same script. • Use the source element to link to the script file. <script src="myjavascript.js” type="text/javascript"> </script> ©Copyright 2014 by Manisha All Rights Reserved.
  • 5. Internal JavaScript • When specifying a script only the tags <script> and </script> are essential, but complete specification is recommended: <script language="javascript” type="text/javascript"> <!-- Begin hiding window.location=”index.html" // End hiding script--> </script> ©Copyright 2014 by Manisha All Rights Reserved.
  • 6. Using Comment Tags • HTML comment tags should bracket any script. • The <!-- script here --> tags hide scripts in HTML and prevent scripts from displaying in browsers that do not interpret JavaScript. • Double slashes // are the signal characters for a JavaScript single-line comment. ©Copyright 2014 by Manisha All Rights Reserved.
  • 7. Inline JavaScript • Event handlers like onLoad are a perfect example of an easy to add tag script. e.g. <body onLoad="alert('WELCOME')"> ©Copyright 2014 by Manisha All Rights Reserved.
  • 8. JavaScript Terminology • Objects, • Properties, • Functions, • Methods, • Events, • Listeners, • Variables. ©Copyright 2014 by Manisha All Rights Reserved.
  • 9. Objects • Objects refers to windows, documents, images, tables, forms, buttons or links, etc. • Objects should be named. • Objects have properties that act as modifiers. ©Copyright 2014 by Manisha All Rights Reserved.
  • 10. Properties • Properties are object attributes. • Object properties are defined by using the object's name, a period, and the property name. • e.g., background color is expressed by: document.bgcolor. Here, document is the object. bgcolor is the property. ©Copyright 2014 by Manisha All Rights Reserved.
  • 11. Functions • A group of statements that is put together once and then can be used repeatedly on a Web page. • JavaScript has built-in functions, and you can write your own. ©Copyright 2014 by Manisha All Rights Reserved.
  • 12. Format of a function definition function function-name( parameter-list ) { declarations and statements } Here, • Function name is any valid identifier. • Parameter list contains names of variables that will receive arguments. • Declarations and statements are function body (“block” of code) ©Copyright 2014 by Manisha All Rights Reserved.
  • 13. Methods • Methods are functions. • They are unusual in the sense that they are stored as properties of objects. • e.g., document.write(”Hello World") Here, document is the object. write is the method. ©Copyright 2014 by Manisha All Rights Reserved.
  • 14. Events • Events are visitor’s and browser’s activities. • There can be two types of events: • User-Initiated Events • Browser-Initiated Events ©Copyright 2014 by Manisha All Rights Reserved.
  • 15. Browser-Initiated Events • load • unload • error • abort ©Copyright 2014 by Manisha All Rights Reserved.
  • 16. User-Initiated Events • click • dblclick • keydown • keyup • keypress • mouseover • mouseout • mousedown • mouseup • mousemove • change • resize • scroll • select • blur • focus • submit ©Copyright 2014 by Manisha All Rights Reserved.
  • 17. Listeners • Listeners capture and actually respond to those events. Thus, they are also known as Event Handlers. • The system sends events to the listener program and the program responds to them as they arrive. ©Copyright 2014 by Manisha All Rights Reserved.
  • 18. Listeners (Cont.) • Events handlers are placed in the BODY part of a Web page as attributes in HTML tags. e.g. <input type=button name=btn1 value=“Click Me” onClick=“alert(‘button was clicked’);”> • Alternatively, events can be captured in the HTML code, and then directed to a JavaScript function for an appropriate response. ©Copyright 2014 by Manisha All Rights Reserved.
  • 19. Listeners (Cont.) e.g. <head> <script language= "JavaScript" type="text/javascript"> function mymessage() { alert(“Hello User!"); } </script> </head> <body onload="mymessage()"> </body> ©Copyright 2014 by Manisha All Rights Reserved.
  • 20. Various Event Handlers • The following event handlers are available in JavaScript: • onAbort • onBlur • onChange • onClick • onDragDrop • onError • onFocus • onKeyDown • onKeyPress • onKeyUp • onSubmit • onMouseDown • onMouseMove • onMouseOut • onMouseOver • onMouseUp • onResize • onSelect • onSubmit • onLoad • onUnload ©Copyright 2014 by Manisha All Rights Reserved.
  • 21. Various Event Handlers (Cont.) • onAbort An abort event occurs when a user aborts the loading of an image (for example by clicking a link or clicking the Stop button). • onBlur A blur event occurs when object on a form loses focus. • onChange A change event occurs when any object (select, text, or textarea) loses focus and its value has been modified. • onClick A click event occurs when an object on a form is clicked. ©Copyright 2014 by Manisha All Rights Reserved.
  • 22. Various Event Handlers (Cont.) • onDragDrop A drapDrop event occurs when a user drops an object onto the browser window, such as dropping a file. • onError An error event occurs when the loading of a document or image causes an error. • onFocus A focus event occurs when a field receives input focus by tabbing with the keyboard or clicking with the mouse. • onKeyDown, onKeyPress, onKeyUp A keyDown, keyPress, or keyUp event occurs when a user depresses a key, presses or holds down a key, or releases a key, respectively. ©Copyright 2014 by Manisha All Rights Reserved.
  • 23. Various Event Handlers (Cont.) • onMouseDown, onMouseMove, onMouseOut, onMouseOver, and onMouseUp A MouseDown, MouseMove, MouseOut, MouseOver, or MouseUp event occurs when a user depresses a mouse button, moves a cursor, moves a cursor out of the object, moves a cursor over the object, releases a mouse button, respectively. • onResize A Resize event occurs when a user or script resizes a window. ©Copyright 2014 by Manisha All Rights Reserved.
  • 24. Various Event Handlers (Cont.) • onSelect A select event occurs when a user selects some of the text within a text or textarea field. • onSubmit A submit event occurs when a user submits a form. • onLoad A load event occurs after a web page is loaded. • onUnload An unload event occurs when you exit a web page. ©Copyright 2014 by Manisha All Rights Reserved.
  • 25. Event Handlers and the “this” Keyword • ‘This’ keyword is used for self-reference of object when an event handler calls a JavaScript function. e.g. <input type="button" value="press me" onclick = "callfunc(this);"> // Here ‘this’ keyword refers to the Button object. ©Copyright 2014 by Manisha All Rights Reserved.
  • 26. Variables • Variables contain values and use the equal sign to specify their value. • Variables are created by declaration using the var command with or without an initial value state. • e.g. var month; • e.g. var month = April; ©Copyright 2014 by Manisha All Rights Reserved.
  • 27. Dialog Boxes • Dialog boxes can be used to – • inform the user of errors or events • confirm actions initiated by the user • ask some information from the user ©Copyright 2014 by Manisha All Rights Reserved.
  • 28. Alert Dialog Box • Alert Dialog Box informs the user about errors or events. User can just read the message and press OK. e.g. alert(‘Hello! Welcome to my website.'); ©Copyright 2014 by Manisha All Rights Reserved.
  • 29. Confirm Dialog Box • Confirm Dialog Box confirms actions initiated by the user. User can confirm or Cancel the action. e.g. confirm('Are you sure you want to exit?'); ©Copyright 2014 by Manisha All Rights Reserved.
  • 30. Prompt Dialog Box • Prompt Dialog Box is used to get information from the user. e.g. prompt('Enter your name:'); ©Copyright 2014 by Manisha All Rights Reserved.
  • 31. Accessing HTML Elements In JavaScript • In JavaScript, you can access specific HTML elements by using a pre-defined variable called document and requesting the element by ID. • Accessing an HTML element by ID, general syntax: document.getElementById("<ID>") e.g. document.getElementById(“d1”).innerHTML=“Hello World!”; <div id=“d1”></div> ©Copyright 2014 by Manisha All Rights Reserved.
  • 32. Accessing HTML Elements In JavaScript (Cont.) • Once you have access to a particular HTML element, you can access all its attributes using the "dot" operator. • Accessing an element's attribute, general syntax: document.getElementById("<ID>").<attribute> • Accessing form element, general syntax: document.formname.elementname.value ©Copyright 2014 by Manisha All Rights Reserved.
  • 33. Example <form name="alertform"> Enter your name: <input type="text" name="yourname"> <input type="button" value= "Go" onClick="window.alert('Hello ' + document.alertform.yourname.value);"> </form> ©Copyright 2014 by Manisha All Rights Reserved.
  • 34. Accessing CSS Properties • Accessing CSS properties, general syntax: document.getElementById("<ID>").style.<property> • Property names in JavaScript are identical to those in CSS, but with namesLikeThis instead of names-like-this e.g. backgroundColor instead of background-color • Example: document.getElementById("name").style.backgroundColor = "yellow"; ©Copyright 2014 by Manisha All Rights Reserved.
  • 35. Form Validations • JavaScript is very good at processing and validating user input in the web browser. • Requirements to perform validations: • Form’s name must be specified. • Object’s name must be specified. • Input must be of Submit type. • Validations can be applied in two ways: 1. Traditional iterative programming methodologies 2. Pattern matching (via regular expressions) ©Copyright 2014 by Manisha All Rights Reserved.
  • 36. Traditional iterative programming methodologies • Write a small program (function) that iterates through the user input and validates the data type and returns true or false depending on result. • This can be relatively slow, especially if form has many fields to be validated. ©Copyright 2014 by Manisha All Rights Reserved.
  • 37. Example function validate() { if(document.login.uname.value==“”) { alert(“Please enter username”); document.login.uname.focus(); return false; } return true; } <form name=“login” action=“” onsubmit=“validate()”> <input type=“text” name=“uname”> ©Copyright 2014 by Manisha All Rights Reserved.
  • 38. Pattern matching • Create a regular expression pattern for the data type and let the system validate the user input via its pattern matching capabilities. • Pattern matching is very quick (even for long forms). ©Copyright 2014 by Manisha All Rights Reserved.
  • 39. HTML5 Validation • required <input type=“text” name=“uname” required/> • pattern= ‘a regular expression’ like text, search, url, telephone, email, and password. <input type=“email” name=“email”/> • min= max = step= can be used with <input type=range> <input type=“range” max=“30”> ©Copyright 2014 by Manisha All Rights Reserved.