SlideShare a Scribd company logo
 
What is JavaScript JavaScript was designed to add interactivity to HTML pages  JavaScript is a scripting language (a scripting language is a lightweight programming language)  A JavaScript consists of lines of executable computer code  A JavaScript is usually embedded directly into HTML pages  JavaScript is an interpreted language (means that scripts execute without preliminary compilation)  Everyone can use JavaScript without purchasing a license
How javascript helps? Javascript gives designer a programming tool It can put dynamic text into HTML page Can respond to events – mouse over, click, page load etc Can read, write and manipulate HTML elements Can help validate use input Can detect version of browser
First Javascript <html> <body> <script type=&quot;text/javascript&quot;> document.write(&quot;Hello World!&quot;) </script> </body> </html>   Script Tag Indicates browser that it’s script that needs to be executed Type attributes indicates what type of script is this? This  is standard command to write stuff on webpage
Place for Javascript Java script can put either in ‘head’ section of html page or in ‘body’ section of html page. If put in body section – the script will be executed automatically when the page is loaded If it is put in head section – then script has to be called explicitly. By putting script in head you ensure that script is loaded first even before any part page uses it
Example <html> <head> <script type=text/javascript> </script> </head> <body> </body> </html> <html> <head> </head> <body> <script type=text/javascript> </script> </body> </html> Javascript in Head Javascript in body
Javascript in separate file Javascript can be present in separate file as well. You only need to refer it in your html. You would refer it in body if you want it to get executed on page load.  You would refer it in head if you want to execute it on an event. Files containing javascript has .js extention
Example <html> <head> <script src= myjscript.js > </script> </head> <body> </body> </html> <html> <head> </head> <body> <script  src=myjscript.js > </script> </body> </html> Javascript in Head Javascript in body
Javascript Quick Stuff Variable Need not put data type for variables. Variables are interpreted based on the values assigned. var  x = 10; var city = “Bangalore” Variables names are case sensitive
if(condition) { } if(condition) { } else { }
String Operations Var x = &quot;It is lot of pressure&quot; Var y = &quot;at Bredge&quot; Var z = x+y  => &quot;It is lot of pressure at Bredge&quot;
Loops For Loop: for(x=0; x <= 10; x++) { } While Loop: while(condition { } Do-while Loop: do { } while(condition) For-In for(variable in object) { }
For – In example <html> <body> <script type=&quot;text/javascript&quot;> var xvar mycars = new Array() mycars[0] = &quot;Saab&quot; mycars[1] = &quot;Volvo&quot; mycars[2] = &quot;BMW &quot; for (x in mycars) { document.write(mycars[x] + &quot;<br />&quot;) } </script> </body> </html>
Javascript popup boxes With javascript we can create popup boxes Three types of popup boxes Alert Box  Confirm Box Prompt Box
Example -AlertBox <html> <head> <script type=&quot;text/javascript&quot;> function disp_alert() { alert(&quot;I am an alert box!!&quot;) } </script> </head> <body> <input type=&quot;button&quot; onclick=&quot;disp_alert()&quot; value=&quot;Display alert box&quot; /> </body> </html>
Example Confirm Box <html> <head> <script type=&quot;text/javascript&quot;> function disp_confirm() { var r=confirm(&quot;Press a button&quot;) if (r==true) { document.write(&quot;You pressed OK!&quot;) } else { document.write(&quot;You pressed Cancel!&quot;) } } </script> </head> <body> <input type=&quot;button&quot; onclick=&quot;disp_confirm()&quot; value=&quot;Display a confirm box&quot; /> </body> </html>
Prompt Box <html> <head> <script type=&quot;text/javascript&quot;> function disp_prompt()   {   var name=prompt(&quot;Please enter your name&quot;,&quot;Harry Potter&quot;)   if (name!=null && name!=&quot;&quot;) { document.write(&quot;Hello &quot; + name + &quot;! How are you today?&quot;) }   } </script> </head> <body> <input type=&quot;button&quot; onclick=&quot;disp_prompt()&quot; value=&quot;Display a prompt box&quot; /> </body> </html>
Functions in JavaScript Functions are written in head section Functions do not have return type However function can return values Functions can take of arguments function  functionname ( var1,var2,...,varX ) { some code }   Functions are called upon document events such as onclick, mouseover and so on.
Function Example <html> <head> <script type=&quot;text/javascript&quot;> function product(a,b) { return a*b } </script> </head> <body> <script type=&quot;text/javascript&quot;> document.write(product(4,3)) </script> </body> </html>
Javascript events Every element of web page such as <body>,<table>,<form>,<input> and so on have events associated with them. When these events occur, a javascript piece of code or javascript function can be invoked Some of the important events are Mouse click  Webpage loading or image loading Mouse over Submitting a page
 
Example - Events <html> <head> <script type=text/javascript> function f1(msg) { alert(msg); } </script> </head> <body> <b>This example also shows bit layouting. You lay out components using table with border=0</b> <table> <tr> <td>Name</td> <td><input type='text' value=&quot;click me&quot; onclick=&quot;f1('you clicked on Name')&quot;></input></td> </tr> <tr> <td>Age</td> <td><input type=text value=&quot;double click here&quot; ondblclick=&quot;f1('you double clicked on Age')&quot;> </input></td> </tr> <tr> <td>Salary</td> <td><input type=text  value=&quot;type somethig here&quot; onchange=&quot;f1('you typed some data in Salary')&quot;> </input></td> </tr> </table> </body> </html>
onerror event Onerror event is fired when there is a script error Since script are not compiled, it will difficult catch typo and other mistakes.  Handling onerror will help identify the error. However this event is fired when the erroneous script is executed To handle this error we need provide a function which handles it. Onerror event provides three information – error message, url of the page where error is caused, line number where error is present. The handling function should take three parameters
Onerror Example <html> <head> <script type=&quot;text/javascript&quot;> onerror=handleErr var txt=&quot;&quot; function handleErr(msg,url,l) { txt=&quot;There was an error on this page.\n\n&quot; txt+=&quot;Error: &quot; + msg + &quot;\n&quot; txt+=&quot;URL: &quot; + url + &quot;\n&quot; txt+=&quot;Line: &quot; + l + &quot;\n\n&quot; txt+=&quot;Click OK to continue.\n\n&quot; alert(txt) return true } function message() { alert(&quot;Welcome guest!&quot;) } </script> </head> <body> <input type=&quot;button&quot; value=&quot;View message&quot; onclick=&quot;message()&quot; /> </body> </html>
Accessing Elements of Page Most often javascript need to access your elemets to validate information to Modify an attribute of an element to Dynamically put the values to add additional elements Element could be any html element such as <body>, <table>,<input>, <form> etc.
Accessing Elements of Page All the elements in a html document are stores like tree with <html></html> being root node Every element can be uniquely identified by giving unit id <intpu type=text id=  &quot; nameField &quot;  > All the attribute values can obtained using document object  var ele =   document.getElementById( &quot; nameField &quot; ) Attribute values of element can be accessed using ‘element’ variable ele.value()
Accessing Elements - Example <html> <head> <script type=text/javascript> function infoRead() {   ele1 = document.getElementById(&quot;name&quot;);   ele2 = document.getElementById(&quot;Age&quot;);   ele3 = document.getElementById(&quot;Salary&quot;);   alert(&quot;You have entered\n&quot;+   &quot;Name - &quot;+ele1.value +   &quot;\nAge - &quot; + ele2.value +   &quot;\nSalary - &quot;+ele3.value   ) } </script> </head> <body> <b>This example also shows bit layouting. You lay out components using table with border=0</b> <table>   <tr>   <td>Name</td> <td><input type='text' id=&quot;name&quot;></input></td>   </tr>   <tr>   <td>Age</td> <td><input type=text id=&quot;Age&quot;> </input></td>   </tr>   <tr>   <td>Salary</td> <td><input type=text  id=&quot;Salary&quot;> </input></td>   </tr> </table> <input type=submit onclick=&quot;infoRead()&quot; value=&quot;Sumit&quot;/> </body> </html>
Javascript Objects javascript is a object oriented language You can create your own objects Every object has property and methods javascript also provide predefined objects String object Date Object Array Object Math Object
Javascript DOM Objects HTML DOM – HTML  D ocument  O bject  M odel It defines standard set of objects for HTML and standard way to access them All HTML elements, their attributes and containing test can be manipulated using DOM
DOM object list
 
 
 
 
 
 
 
 
 
 
 
 
Ad

Recommended

Java Script
Java Script
husbancom
 
Java script
Java script
umesh patil
 
Java script
Java script
umesh patil
 
Java script
Java script
ITz_1
 
Introduction to Java Script
Introduction to Java Script
Vijay Kumar Verma
 
JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1
Gene Babon
 
JAVA SCRIPT
JAVA SCRIPT
Go4Guru
 
Java Script An Introduction By HWA
Java Script An Introduction By HWA
Emma Wood
 
1. java script language fundamentals
1. java script language fundamentals
Rajiv Gupta
 
Java Script
Java Script
Dr. SURBHI SAROHA
 
Session vii(java scriptbasics)
Session vii(java scriptbasics)
Shrijan Tiwari
 
Introduction to Javascript programming
Introduction to Javascript programming
Fulvio Corno
 
Introduction to java_script
Introduction to java_script
Basavaraj Hampali
 
Java script by Act Academy
Java script by Act Academy
actanimation
 
Basics java scripts
Basics java scripts
ch samaram
 
Java script
Java script
reddivarihareesh
 
Java script writing javascript
Java script writing javascript
Jesus Obenita Jr.
 
Java script
Java script
Fajar Baskoro
 
Web designing unit 4
Web designing unit 4
Dr. SURBHI SAROHA
 
Javascripts. pptt
Javascripts. pptt
RaviShankarSinghal
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Javascript
Javascript
padmaashree Arunachalem
 
Test2
Test2
Ermias Hailemicheal
 
Java script programs
Java script programs
ITz_1
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
Introduction To JavaScript
Introduction To JavaScript
Reema
 
Introduction of javascript
Introduction of javascript
syeda zoya mehdi
 
JavaScript - Part-1
JavaScript - Part-1
Jainul Musani
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2
lm092068
 

More Related Content

What's hot (20)

1. java script language fundamentals
1. java script language fundamentals
Rajiv Gupta
 
Java Script
Java Script
Dr. SURBHI SAROHA
 
Session vii(java scriptbasics)
Session vii(java scriptbasics)
Shrijan Tiwari
 
Introduction to Javascript programming
Introduction to Javascript programming
Fulvio Corno
 
Introduction to java_script
Introduction to java_script
Basavaraj Hampali
 
Java script by Act Academy
Java script by Act Academy
actanimation
 
Basics java scripts
Basics java scripts
ch samaram
 
Java script
Java script
reddivarihareesh
 
Java script writing javascript
Java script writing javascript
Jesus Obenita Jr.
 
Java script
Java script
Fajar Baskoro
 
Web designing unit 4
Web designing unit 4
Dr. SURBHI SAROHA
 
Javascripts. pptt
Javascripts. pptt
RaviShankarSinghal
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Javascript
Javascript
padmaashree Arunachalem
 
Test2
Test2
Ermias Hailemicheal
 
Java script programs
Java script programs
ITz_1
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
Introduction To JavaScript
Introduction To JavaScript
Reema
 
Introduction of javascript
Introduction of javascript
syeda zoya mehdi
 
JavaScript - Part-1
JavaScript - Part-1
Jainul Musani
 

Viewers also liked (20)

The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2
lm092068
 
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
Event Handler
 
An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5
Event Handler
 
Unchallengeable miracle of Holy Quran
Unchallengeable miracle of Holy Quran
yoursincerefriend
 
8. java script
8. java script
AnusAhmad
 
The big bang theory of social recruiting
The big bang theory of social recruiting
FastCollab
 
An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4
Event Handler
 
An Introduction to JavaScript: Week 3
An Introduction to JavaScript: Week 3
Event Handler
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
Event Handler
 
Big Bang Theory
Big Bang Theory
Kevin James
 
Java script -23jan2015
Java script -23jan2015
Sasidhar Kothuru
 
Chapter 1 - How the world begin
Chapter 1 - How the world begin
Green Pond Baptist Church
 
Big Bang Theorychandler
Big Bang Theorychandler
guest008d7bd
 
Qur’an and its sciences
Qur’an and its sciences
Kalsoom Mohammed
 
Large-Scale JavaScript Development
Large-Scale JavaScript Development
Addy Osmani
 
Java script Learn Easy
Java script Learn Easy
prince Loffar
 
The Quran and Computational Linguistics
The Quran and Computational Linguistics
Abdul Baquee Muhammad Sharaf
 
Evolution of universe
Evolution of universe
Anmol Marya
 
Quranic concept of human life cycle urdu
Quranic concept of human life cycle urdu
Islamic Studies Program
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2
lm092068
 
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
Event Handler
 
An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5
Event Handler
 
Unchallengeable miracle of Holy Quran
Unchallengeable miracle of Holy Quran
yoursincerefriend
 
8. java script
8. java script
AnusAhmad
 
The big bang theory of social recruiting
The big bang theory of social recruiting
FastCollab
 
An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4
Event Handler
 
An Introduction to JavaScript: Week 3
An Introduction to JavaScript: Week 3
Event Handler
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
Event Handler
 
Big Bang Theorychandler
Big Bang Theorychandler
guest008d7bd
 
Large-Scale JavaScript Development
Large-Scale JavaScript Development
Addy Osmani
 
Java script Learn Easy
Java script Learn Easy
prince Loffar
 
Evolution of universe
Evolution of universe
Anmol Marya
 
Quranic concept of human life cycle urdu
Quranic concept of human life cycle urdu
Islamic Studies Program
 
Ad

Similar to Java Script (20)

KMUTNB - Internet Programming 3/7
KMUTNB - Internet Programming 3/7
phuphax
 
JWU Guest Talk: JavaScript and AJAX
JWU Guest Talk: JavaScript and AJAX
Hilary Mason
 
Javascript: Ajax & DOM Manipulation v1.2
Javascript: Ajax & DOM Manipulation v1.2
borkweb
 
Developing and testing ajax components
Developing and testing ajax components
Ignacio Coloma
 
Developing Gadgets
Developing Gadgets
Quirk
 
JavaScript
JavaScript
Doncho Minkov
 
Vb.Net Web Forms
Vb.Net Web Forms
Dutch Dasanaike {LION}
 
Javascript Basic
Javascript Basic
Kang-min Liu
 
JSP Custom Tags
JSP Custom Tags
BG Java EE Course
 
Lecture 5 - Comm Lab: Web @ ITP
Lecture 5 - Comm Lab: Web @ ITP
yucefmerhi
 
Week7
Week7
H K
 
HTML Fundamentals
HTML Fundamentals
Doncho Minkov
 
YL Intro html
YL Intro html
dilom1986
 
Everything You Always Wanted To Know About XML But Were Afraid To Ask
Everything You Always Wanted To Know About XML But Were Afraid To Ask
Richard Davis
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 
Migration testing framework
Migration testing framework
IndicThreads
 
Ajax ons2
Ajax ons2
Chad Davis
 
Introduction To Lamp
Introduction To Lamp
Amzad Hossain
 
Introduction to html
Introduction to html
vikasgaur31
 
Introduction to html
Introduction to html
vikasgaur31
 
KMUTNB - Internet Programming 3/7
KMUTNB - Internet Programming 3/7
phuphax
 
JWU Guest Talk: JavaScript and AJAX
JWU Guest Talk: JavaScript and AJAX
Hilary Mason
 
Javascript: Ajax & DOM Manipulation v1.2
Javascript: Ajax & DOM Manipulation v1.2
borkweb
 
Developing and testing ajax components
Developing and testing ajax components
Ignacio Coloma
 
Developing Gadgets
Developing Gadgets
Quirk
 
Lecture 5 - Comm Lab: Web @ ITP
Lecture 5 - Comm Lab: Web @ ITP
yucefmerhi
 
Week7
Week7
H K
 
YL Intro html
YL Intro html
dilom1986
 
Everything You Always Wanted To Know About XML But Were Afraid To Ask
Everything You Always Wanted To Know About XML But Were Afraid To Ask
Richard Davis
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 
Migration testing framework
Migration testing framework
IndicThreads
 
Introduction To Lamp
Introduction To Lamp
Amzad Hossain
 
Introduction to html
Introduction to html
vikasgaur31
 
Introduction to html
Introduction to html
vikasgaur31
 
Ad

Recently uploaded (20)

Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 

Java Script

  • 1.  
  • 2. What is JavaScript JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight programming language) A JavaScript consists of lines of executable computer code A JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a license
  • 3. How javascript helps? Javascript gives designer a programming tool It can put dynamic text into HTML page Can respond to events – mouse over, click, page load etc Can read, write and manipulate HTML elements Can help validate use input Can detect version of browser
  • 4. First Javascript <html> <body> <script type=&quot;text/javascript&quot;> document.write(&quot;Hello World!&quot;) </script> </body> </html> Script Tag Indicates browser that it’s script that needs to be executed Type attributes indicates what type of script is this? This is standard command to write stuff on webpage
  • 5. Place for Javascript Java script can put either in ‘head’ section of html page or in ‘body’ section of html page. If put in body section – the script will be executed automatically when the page is loaded If it is put in head section – then script has to be called explicitly. By putting script in head you ensure that script is loaded first even before any part page uses it
  • 6. Example <html> <head> <script type=text/javascript> </script> </head> <body> </body> </html> <html> <head> </head> <body> <script type=text/javascript> </script> </body> </html> Javascript in Head Javascript in body
  • 7. Javascript in separate file Javascript can be present in separate file as well. You only need to refer it in your html. You would refer it in body if you want it to get executed on page load. You would refer it in head if you want to execute it on an event. Files containing javascript has .js extention
  • 8. Example <html> <head> <script src= myjscript.js > </script> </head> <body> </body> </html> <html> <head> </head> <body> <script src=myjscript.js > </script> </body> </html> Javascript in Head Javascript in body
  • 9. Javascript Quick Stuff Variable Need not put data type for variables. Variables are interpreted based on the values assigned. var x = 10; var city = “Bangalore” Variables names are case sensitive
  • 10. if(condition) { } if(condition) { } else { }
  • 11. String Operations Var x = &quot;It is lot of pressure&quot; Var y = &quot;at Bredge&quot; Var z = x+y => &quot;It is lot of pressure at Bredge&quot;
  • 12. Loops For Loop: for(x=0; x <= 10; x++) { } While Loop: while(condition { } Do-while Loop: do { } while(condition) For-In for(variable in object) { }
  • 13. For – In example <html> <body> <script type=&quot;text/javascript&quot;> var xvar mycars = new Array() mycars[0] = &quot;Saab&quot; mycars[1] = &quot;Volvo&quot; mycars[2] = &quot;BMW &quot; for (x in mycars) { document.write(mycars[x] + &quot;<br />&quot;) } </script> </body> </html>
  • 14. Javascript popup boxes With javascript we can create popup boxes Three types of popup boxes Alert Box Confirm Box Prompt Box
  • 15. Example -AlertBox <html> <head> <script type=&quot;text/javascript&quot;> function disp_alert() { alert(&quot;I am an alert box!!&quot;) } </script> </head> <body> <input type=&quot;button&quot; onclick=&quot;disp_alert()&quot; value=&quot;Display alert box&quot; /> </body> </html>
  • 16. Example Confirm Box <html> <head> <script type=&quot;text/javascript&quot;> function disp_confirm() { var r=confirm(&quot;Press a button&quot;) if (r==true) { document.write(&quot;You pressed OK!&quot;) } else { document.write(&quot;You pressed Cancel!&quot;) } } </script> </head> <body> <input type=&quot;button&quot; onclick=&quot;disp_confirm()&quot; value=&quot;Display a confirm box&quot; /> </body> </html>
  • 17. Prompt Box <html> <head> <script type=&quot;text/javascript&quot;> function disp_prompt() { var name=prompt(&quot;Please enter your name&quot;,&quot;Harry Potter&quot;) if (name!=null && name!=&quot;&quot;) { document.write(&quot;Hello &quot; + name + &quot;! How are you today?&quot;) } } </script> </head> <body> <input type=&quot;button&quot; onclick=&quot;disp_prompt()&quot; value=&quot;Display a prompt box&quot; /> </body> </html>
  • 18. Functions in JavaScript Functions are written in head section Functions do not have return type However function can return values Functions can take of arguments function functionname ( var1,var2,...,varX ) { some code } Functions are called upon document events such as onclick, mouseover and so on.
  • 19. Function Example <html> <head> <script type=&quot;text/javascript&quot;> function product(a,b) { return a*b } </script> </head> <body> <script type=&quot;text/javascript&quot;> document.write(product(4,3)) </script> </body> </html>
  • 20. Javascript events Every element of web page such as <body>,<table>,<form>,<input> and so on have events associated with them. When these events occur, a javascript piece of code or javascript function can be invoked Some of the important events are Mouse click Webpage loading or image loading Mouse over Submitting a page
  • 21.  
  • 22. Example - Events <html> <head> <script type=text/javascript> function f1(msg) { alert(msg); } </script> </head> <body> <b>This example also shows bit layouting. You lay out components using table with border=0</b> <table> <tr> <td>Name</td> <td><input type='text' value=&quot;click me&quot; onclick=&quot;f1('you clicked on Name')&quot;></input></td> </tr> <tr> <td>Age</td> <td><input type=text value=&quot;double click here&quot; ondblclick=&quot;f1('you double clicked on Age')&quot;> </input></td> </tr> <tr> <td>Salary</td> <td><input type=text value=&quot;type somethig here&quot; onchange=&quot;f1('you typed some data in Salary')&quot;> </input></td> </tr> </table> </body> </html>
  • 23. onerror event Onerror event is fired when there is a script error Since script are not compiled, it will difficult catch typo and other mistakes. Handling onerror will help identify the error. However this event is fired when the erroneous script is executed To handle this error we need provide a function which handles it. Onerror event provides three information – error message, url of the page where error is caused, line number where error is present. The handling function should take three parameters
  • 24. Onerror Example <html> <head> <script type=&quot;text/javascript&quot;> onerror=handleErr var txt=&quot;&quot; function handleErr(msg,url,l) { txt=&quot;There was an error on this page.\n\n&quot; txt+=&quot;Error: &quot; + msg + &quot;\n&quot; txt+=&quot;URL: &quot; + url + &quot;\n&quot; txt+=&quot;Line: &quot; + l + &quot;\n\n&quot; txt+=&quot;Click OK to continue.\n\n&quot; alert(txt) return true } function message() { alert(&quot;Welcome guest!&quot;) } </script> </head> <body> <input type=&quot;button&quot; value=&quot;View message&quot; onclick=&quot;message()&quot; /> </body> </html>
  • 25. Accessing Elements of Page Most often javascript need to access your elemets to validate information to Modify an attribute of an element to Dynamically put the values to add additional elements Element could be any html element such as <body>, <table>,<input>, <form> etc.
  • 26. Accessing Elements of Page All the elements in a html document are stores like tree with <html></html> being root node Every element can be uniquely identified by giving unit id <intpu type=text id= &quot; nameField &quot; > All the attribute values can obtained using document object var ele = document.getElementById( &quot; nameField &quot; ) Attribute values of element can be accessed using ‘element’ variable ele.value()
  • 27. Accessing Elements - Example <html> <head> <script type=text/javascript> function infoRead() { ele1 = document.getElementById(&quot;name&quot;); ele2 = document.getElementById(&quot;Age&quot;); ele3 = document.getElementById(&quot;Salary&quot;); alert(&quot;You have entered\n&quot;+ &quot;Name - &quot;+ele1.value + &quot;\nAge - &quot; + ele2.value + &quot;\nSalary - &quot;+ele3.value ) } </script> </head> <body> <b>This example also shows bit layouting. You lay out components using table with border=0</b> <table> <tr> <td>Name</td> <td><input type='text' id=&quot;name&quot;></input></td> </tr> <tr> <td>Age</td> <td><input type=text id=&quot;Age&quot;> </input></td> </tr> <tr> <td>Salary</td> <td><input type=text id=&quot;Salary&quot;> </input></td> </tr> </table> <input type=submit onclick=&quot;infoRead()&quot; value=&quot;Sumit&quot;/> </body> </html>
  • 28. Javascript Objects javascript is a object oriented language You can create your own objects Every object has property and methods javascript also provide predefined objects String object Date Object Array Object Math Object
  • 29. Javascript DOM Objects HTML DOM – HTML D ocument O bject M odel It defines standard set of objects for HTML and standard way to access them All HTML elements, their attributes and containing test can be manipulated using DOM
  • 31.  
  • 32.  
  • 33.  
  • 34.  
  • 35.  
  • 36.  
  • 37.  
  • 38.  
  • 39.  
  • 40.  
  • 41.  
  • 42.