SlideShare a Scribd company logo
1 of 19
Download to read offline
13-05-2013Rajavel DRajavel D
JavaScript
Summer Internship – 2013
(Indian Institute of Technology Bombay)
JavaScript
 JavaScript is a lightweight scripting language
 Embedded directly into HTML pages
 JavaScript is an interpreted language (means
that scripts execute without preliminary
compilation)
Rajavel D JavaScript IITB-CSE-Internship 2013
What can a JavaScript do?
 Client side data validation
 React to event on html element
 Read and write the html element
 Put dynamic content on html pages
 Creates the client side persistent (cookies, session )
Rajavel D JavaScript IITB-CSE-Internship 2013
Sample JavaScript
<html>
<body>
<p id="demo"></p>
<script type="text/javascript">
document.getElementById("demo").innerHTML=Date();
</script>
</body>
</html>
Rajavel D JavaScript IITB-CSE-Internship 2013
JavaScript in <head>
<html> <head>
<script type="text/javascript">
function displayDate()
{ document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body> <p id="demo"></p>
<button type="button" onclick="displayDate()">Display
Date</button>
</body> </html>
Rajavel D JavaScript IITB-CSE-Internship 2013
Using an External JavaScript
<head>
<script type="text/javascript" src=“myjavascript.js"></script>
</head>
 JavaScript Statements
 The semicolon is optional
 Using semicolons makes it possible to write multiple statements
on one line.
 Each statement is executed by the browser in the sequence
they are written
Rajavel D JavaScript IITB-CSE-Internship 2013
Comments and Popup Boxes
 JavaScript Comments
 Single line comments start with //.
 Multi line comments start with /* and end with */.
 JavaScript Popup Boxes
 alert("I am an alert box!");
 var r=confirm("Press a button");
 r = true | false
 var name=prompt("Please enter your name","");
Rajavel D JavaScript IITB-CSE-Internship 2013
JavaScript Events
 onLoad and onUnload
 onFocus, onBlur and onChange
 OnSubmit
 onMouseOver, onmousedown, onmouseout
 Onerror
 Onkeydown, onkeyup
 onselect
Rajavel D JavaScript IITB-CSE-Internship 2013
Required Fields Validation
function validateForm()
{
var x=document.getElementById("txt").value
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
Rajavel D JavaScript IITB-CSE-Internship 2013
E-mail Validation
function validateForm()
{
var x=document.forms["myForm"]["email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
Rajavel D JavaScript IITB-CSE-Internship 2013
JavaScript Advanced
Timing
setTimeout("MsgAfter3sec()",3000);
clearTimeout();
Object
<script type="text/javascript">
personObj={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"}
document.write(personObj.firstname + " is " + personObj.age + " years old.");
</script>
Rajavel D JavaScript IITB-CSE-Internship 2013
JavaScript Cookies
 A cookie is a variable that is stored on the
visitor's computer
 With JavaScript, you can both create and
retrieve cookie values.
Rajavel D JavaScript IITB-CSE-Internship 2013
Create and Store a Cookie
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : ";
expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
Rajavel D JavaScript IITB-CSE-Internship 2013
Get a Cookie
function getCookie(c_name)
{
var i,x,y,Mycookies=document.cookie.split(";");
for (i=0;i<Mycookies.length;i++)
{
x=Mycookies[i].substr(0,Mycookies[i].indexOf("="));
y=Mycookies[i].substr(Mycookies[i].indexOf("=")+1);
if (x==c_name)
return unescape(y);
} }
Rajavel D JavaScript IITB-CSE-Internship 2013
Check Cookie
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
alert("Welcome again " + username);
else
{ username=prompt("Please enter your name:","");
if (username!=null && username!="")
setCookie("username",username,365);
}
}
Rajavel D JavaScript IITB-CSE-Internship 2013
Document Object
 document.anchors/forms/images.length
 document.anchors[0].innerHTML
 document.title, document.URL
 document.getElementById("myHeader").innerHTML
 document.open("text/html","replace");doc.write(“<html>...</html>”);
 document.getElementsByTagName("input");
 document.getElementById("frm1").reset();
 Event Object
 onmousedown="whichButton(event)", //event.button==2 --> right
 onkeyup="whichButton(event)"//event.keyCode --> ascii
Rajavel D JavaScript IITB-CSE-Internship 2013
CSS on JavaScript
 Changing the dynamic style
 Examples :
 doc.getElementById(“mydiv”).style.Background=“#fdf”
 doc.getElementById("mydiv").style.fontSize= “20px";
 document.getElementById("mydiv").style.color= "#fff";
 doc.getElementById(“div").style.display="block/none";
Rajavel D JavaScript IITB-CSE-Internship 2013
Rajavel D JavaScript IITB-CSE-Internship 2013
Any Doubts ???
References
 www.w3schools.com/js/
 www.tutorialspoint.com/javascript/
Rajavel D JavaScript IITB-CSE-Internship 2013

More Related Content

What's hot

What's hot (20)

Javascript
JavascriptJavascript
Javascript
 
JavaScript Variables
JavaScript VariablesJavaScript Variables
JavaScript Variables
 
Html forms
Html formsHtml forms
Html forms
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
JavaScript Conditional Statements
JavaScript Conditional StatementsJavaScript Conditional Statements
JavaScript Conditional Statements
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Javascript
JavascriptJavascript
Javascript
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
CSS
CSSCSS
CSS
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
HTML and CSS crash course!
HTML and CSS crash course!HTML and CSS crash course!
HTML and CSS crash course!
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Java script
Java scriptJava script
Java script
 
JavaScript
JavaScriptJavaScript
JavaScript
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 

Viewers also liked (6)

Jsp
JspJsp
Jsp
 
Audio video class room
Audio video class roomAudio video class room
Audio video class room
 
Javascript
JavascriptJavascript
Javascript
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
 
Java
JavaJava
Java
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similar to Javascript

Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)Ajay Khatri
 
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scriptingpkaviya
 
Webdesing lab part-b__java_script_
Webdesing lab part-b__java_script_Webdesing lab part-b__java_script_
Webdesing lab part-b__java_script_Shivanand Algundi
 
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 JavascriptArti Parab Academics
 
Implementation of GUI Framework part3
Implementation of GUI Framework part3Implementation of GUI Framework part3
Implementation of GUI Framework part3masahiroookubo
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingHoat Le
 
Ajax for dummies, and not only.
Ajax for dummies, and not only.Ajax for dummies, and not only.
Ajax for dummies, and not only.Nerd Tzanetopoulos
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Developmentvito jeng
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript MisunderstoodBhavya Siddappa
 
Gdg dev fest hybrid apps your own mini-cordova
Gdg dev fest hybrid apps  your own mini-cordovaGdg dev fest hybrid apps  your own mini-cordova
Gdg dev fest hybrid apps your own mini-cordovaAyman Mahfouz
 
단일 페이지 인터페이스 웹/앱 개발
단일 페이지 인터페이스 웹/앱 개발단일 페이지 인터페이스 웹/앱 개발
단일 페이지 인터페이스 웹/앱 개발동수 장
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy prince Loffar
 
e-suap - client technologies- english version
e-suap - client technologies- english versione-suap - client technologies- english version
e-suap - client technologies- english versionSabino Labarile
 
Open Source Ajax Solution @OSDC.tw 2009
Open Source Ajax  Solution @OSDC.tw 2009Open Source Ajax  Solution @OSDC.tw 2009
Open Source Ajax Solution @OSDC.tw 2009Robbie Cheng
 

Similar to Javascript (20)

Java script
Java scriptJava script
Java script
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
 
Webdesing lab part-b__java_script_
Webdesing lab part-b__java_script_Webdesing lab part-b__java_script_
Webdesing lab part-b__java_script_
 
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
 
Implementation of GUI Framework part3
Implementation of GUI Framework part3Implementation of GUI Framework part3
Implementation of GUI Framework part3
 
Java script
Java scriptJava script
Java script
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Ajax for dummies, and not only.
Ajax for dummies, and not only.Ajax for dummies, and not only.
Ajax for dummies, and not only.
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Development
 
Advance java
Advance javaAdvance java
Advance java
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
 
Gdg dev fest hybrid apps your own mini-cordova
Gdg dev fest hybrid apps  your own mini-cordovaGdg dev fest hybrid apps  your own mini-cordova
Gdg dev fest hybrid apps your own mini-cordova
 
Wt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technologyWt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technology
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
 
단일 페이지 인터페이스 웹/앱 개발
단일 페이지 인터페이스 웹/앱 개발단일 페이지 인터페이스 웹/앱 개발
단일 페이지 인터페이스 웹/앱 개발
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
e-suap - client technologies- english version
e-suap - client technologies- english versione-suap - client technologies- english version
e-suap - client technologies- english version
 
Open Source Ajax Solution @OSDC.tw 2009
Open Source Ajax  Solution @OSDC.tw 2009Open Source Ajax  Solution @OSDC.tw 2009
Open Source Ajax Solution @OSDC.tw 2009
 

Recently uploaded

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 

Recently uploaded (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 

Javascript

  • 1. 13-05-2013Rajavel DRajavel D JavaScript Summer Internship – 2013 (Indian Institute of Technology Bombay)
  • 2. JavaScript  JavaScript is a lightweight scripting language  Embedded directly into HTML pages  JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Rajavel D JavaScript IITB-CSE-Internship 2013
  • 3. What can a JavaScript do?  Client side data validation  React to event on html element  Read and write the html element  Put dynamic content on html pages  Creates the client side persistent (cookies, session ) Rajavel D JavaScript IITB-CSE-Internship 2013
  • 4. Sample JavaScript <html> <body> <p id="demo"></p> <script type="text/javascript"> document.getElementById("demo").innerHTML=Date(); </script> </body> </html> Rajavel D JavaScript IITB-CSE-Internship 2013
  • 5. JavaScript in <head> <html> <head> <script type="text/javascript"> function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> </head> <body> <p id="demo"></p> <button type="button" onclick="displayDate()">Display Date</button> </body> </html> Rajavel D JavaScript IITB-CSE-Internship 2013
  • 6. Using an External JavaScript <head> <script type="text/javascript" src=“myjavascript.js"></script> </head>  JavaScript Statements  The semicolon is optional  Using semicolons makes it possible to write multiple statements on one line.  Each statement is executed by the browser in the sequence they are written Rajavel D JavaScript IITB-CSE-Internship 2013
  • 7. Comments and Popup Boxes  JavaScript Comments  Single line comments start with //.  Multi line comments start with /* and end with */.  JavaScript Popup Boxes  alert("I am an alert box!");  var r=confirm("Press a button");  r = true | false  var name=prompt("Please enter your name",""); Rajavel D JavaScript IITB-CSE-Internship 2013
  • 8. JavaScript Events  onLoad and onUnload  onFocus, onBlur and onChange  OnSubmit  onMouseOver, onmousedown, onmouseout  Onerror  Onkeydown, onkeyup  onselect Rajavel D JavaScript IITB-CSE-Internship 2013
  • 9. Required Fields Validation function validateForm() { var x=document.getElementById("txt").value if (x==null || x=="") { alert("First name must be filled out"); return false; } } Rajavel D JavaScript IITB-CSE-Internship 2013
  • 10. E-mail Validation function validateForm() { var x=document.forms["myForm"]["email"].value var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } } Rajavel D JavaScript IITB-CSE-Internship 2013
  • 12. JavaScript Cookies  A cookie is a variable that is stored on the visitor's computer  With JavaScript, you can both create and retrieve cookie values. Rajavel D JavaScript IITB-CSE-Internship 2013
  • 13. Create and Store a Cookie function setCookie(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } Rajavel D JavaScript IITB-CSE-Internship 2013
  • 14. Get a Cookie function getCookie(c_name) { var i,x,y,Mycookies=document.cookie.split(";"); for (i=0;i<Mycookies.length;i++) { x=Mycookies[i].substr(0,Mycookies[i].indexOf("=")); y=Mycookies[i].substr(Mycookies[i].indexOf("=")+1); if (x==c_name) return unescape(y); } } Rajavel D JavaScript IITB-CSE-Internship 2013
  • 15. Check Cookie function checkCookie() { var username=getCookie("username"); if (username!=null && username!="") alert("Welcome again " + username); else { username=prompt("Please enter your name:",""); if (username!=null && username!="") setCookie("username",username,365); } } Rajavel D JavaScript IITB-CSE-Internship 2013
  • 16. Document Object  document.anchors/forms/images.length  document.anchors[0].innerHTML  document.title, document.URL  document.getElementById("myHeader").innerHTML  document.open("text/html","replace");doc.write(“<html>...</html>”);  document.getElementsByTagName("input");  document.getElementById("frm1").reset();  Event Object  onmousedown="whichButton(event)", //event.button==2 --> right  onkeyup="whichButton(event)"//event.keyCode --> ascii Rajavel D JavaScript IITB-CSE-Internship 2013
  • 17. CSS on JavaScript  Changing the dynamic style  Examples :  doc.getElementById(“mydiv”).style.Background=“#fdf”  doc.getElementById("mydiv").style.fontSize= “20px";  document.getElementById("mydiv").style.color= "#fff";  doc.getElementById(“div").style.display="block/none"; Rajavel D JavaScript IITB-CSE-Internship 2013
  • 18. Rajavel D JavaScript IITB-CSE-Internship 2013 Any Doubts ???