SlideShare a Scribd company logo
Javascript
-Varsha Kumari
02/02/19 1
Introduction
• JavaScript is a front-end scripting language
developed by Netscape for dynamic content
– Lightweight, but with limited capabilities
– Can be used as object-oriented language
• Client-side technology
– Embedded in your HTML page
– Interpreted by the Web browser
• Simple and flexible
02/02/19 2
• HTML to define the content of web pages
• CSS to specify the layout of web pages
• JavaScript to program the dynamic behavior
of web pages
02/02/19 3
Using JavaScript Code
• The JavaScript code can be placed in:
– <script> tag in the head </script>
– <script> tag in the body </script>
– External files, linked via <script> tag in the head
• Files usually have .js extension
4
<script src="scripts.js" type="text/javscript"><script src="scripts.js" type="text/javscript">
</script></script>
The First Script
first-script.html
5
<html><html>
<body><body>
<script type="text/javascript"><script type="text/javascript">
alert('Hello JavaScript!');alert('Hello JavaScript!');
</script></script>
</body></body>
</html></html>
Another Small Example
small-example.html
6
<html><html>
<body><body>
<script type="text/javascript"><script type="text/javascript">
document.write('JavaScript rulez!');document.write('JavaScript rulez!');
</script></script>
</body></body>
</html></html>
<html><html>
<head><head>
<script type="text/javascript"><script type="text/javascript">
function test (message) {function test (message) {
alert(message);alert(message);
}}
</script></script>
</head></head>
<body><body>
<img src="logo.gif"<img src="logo.gif"
onclick="test('clicked!')" />onclick="test('clicked!')" />
</body></body>
</html></html>
Calling a JavaScript Function
from Event Handler –
Example
image-onclick.html
7
Using External Script Files• Using external script files:
External JavaScript file:
8
<html><html>
<head><head>
<script src="sample.js" type="text/javascript"><script src="sample.js" type="text/javascript">
</script></script>
</head></head>
<body><body>
<button onclick="sample()" value="Call JavaScript<button onclick="sample()" value="Call JavaScript
function from sample.js" />function from sample.js" />
</body></body>
</html></html>
function sample() {function sample() {
alert('Hello from sample.js!')alert('Hello from sample.js!')
external-JavaScript.htmlexternal-JavaScript.html
sample.jssample.js
The <script> tag is always empty.The <script> tag is always empty.
<script type = "text/JavaScript" language="JavaScript">
var a = "These sentence";
var italics_a = a.italics();
var b = "will continue over";
var upper_b = b.toUpperCase();
var c = "three variables";
var red_c = c.fontcolor("red");
var sentence = a+b+c;
var sentence_change = italics_a+upper_b+red_c;
document.write(sentence);
document.write("<br>");
document.write(sentence_change);
</script>
Standard Popup Boxes
• Alert box with text and [OK] button
– Just a message shown in a dialog box:
• Confirmation box
– Contains text, [OK] button and [Cancel] button:
• Prompt box
– Contains text, input field with default value:
10
alert("Some text here");alert("Some text here");
confirm("Are you sure?");confirm("Are you sure?");
prompt ("enter amount", 10);prompt ("enter amount", 10);
Alert Dialog Box
<html> <head>
<script type="text/javascript">
function Warn() {
alert ("This is a warning message!");
document.write ("This is a warning message!");
}
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form> <input type="button" value="Click Me" onclick="Warn();" />
</form>
</body></html>
Confirmation Dialog Box
<html>
<head>
<script type="text/javascript">
function getConfirmation()
{
var retVal = confirm("Do you want to continue ?");
if( retVal == true )
{
document.write ("User wants to continue!");
return true;
}
Else
{ document.write ("User does not want to continue!");
return false; }
} </script> </head>
<body> <p>Click the following button to see the result: </p> <form>
<input type="button" value="Click Me" onclick="getConfirmation();" /> </form>
</body> </html>
Prompt Dialog Box
<html> <head>
<script type="text/javascript">
function getValue(){
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
} </script> </head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type="button" value="Click Me" onclick="getValue();" />
</form>
</body></html>
JavaScript Syntax
• The JavaScript syntax is similar to Java
– Operators (+, *, =, !=, &&, ++, …)
– Variables (typeless)
– Conditional statements (if, else)
– Loops (for, while)
– Arrays (my_array[]) and associative arrays
(my_array['abc'])
– Functions (can return value)
14
Data Types
• JavaScript data types:
– Numbers (integer, floating-point)
– Boolean (true / false)
• String type – string of characters
• Arrays
• Associative arrays (hash tables)
15
var myName = "You can use both single or doublevar myName = "You can use both single or double
quotes for strings";quotes for strings";
var my_array = [1, 5.3, "aaa"];var my_array = [1, 5.3, "aaa"];
var my_hash = {a:2, b:3, c:"text"};var my_hash = {a:2, b:3, c:"text"};
Everything is Object• Every variable can be considered as object
– For example strings and arrays have member
functions:
16
var test = "some string";var test = "some string";
alert(test[7]); // shows letter 'r'alert(test[7]); // shows letter 'r'
alert(test.charAt(5)); // shows letter 's'alert(test.charAt(5)); // shows letter 's'
alert("test".charAt(1)); //shows letter 'e'alert("test".charAt(1)); //shows letter 'e'
alert("test".substring(1,3)); //shows 'esalert("test".substring(1,3)); //shows 'es''
var arr = [1,3,4];var arr = [1,3,4];
alert (arr.length); // shows 3alert (arr.length); // shows 3
arr.push(7); // appends 7 to end of arrayarr.push(7); // appends 7 to end of array
alert (arr[3]); // shows 7alert (arr[3]); // shows 7
objects.htmlobjects.html
String Operations
The + operator joins strings
• What is "9" + 9?
• Converting string to number:
17
string1 = "fat ";string1 = "fat ";
string2 = "cats";string2 = "cats";
alert(string1 + string2); // fat catsalert(string1 + string2); // fat cats
alert("9" + 9); // 99alert("9" + 9); // 99
alert(parseInt("9") + 9); // 18alert(parseInt("9") + 9); // 18
Sum of Numbers – Example
sum-of-numbers.html
18
<html><html>
<head><head>
<title>JavaScript Demo</title><title>JavaScript Demo</title>
<script type="text/javascript"><script type="text/javascript">
function calcSum() {function calcSum() {
value1 =value1 =
parseInt(document.mainForm.textBox1.value);parseInt(document.mainForm.textBox1.value);
value2 =value2 =
parseInt(document.mainForm.textBox2.value);parseInt(document.mainForm.textBox2.value);
sum = value1 + value2;sum = value1 + value2;
document.mainForm.textBoxSum.value = sum;document.mainForm.textBoxSum.value = sum;
}}
</script></script>
</head></head>
Sum of Numbers – Example (2)
sum-of-numbers.html (cont.)
19
<body><body>
<form name="mainForm"><form name="mainForm">
<input type="text" name="textBox1" /> <br/><input type="text" name="textBox1" /> <br/>
<input type="text" name="textBox2" /> <br/><input type="text" name="textBox2" /> <br/>
<input type="button" value="Process"<input type="button" value="Process"
onclick="javascript: calcSum()" />onclick="javascript: calcSum()" />
<input type="text" name="textBoxSum"<input type="text" name="textBoxSum"
readonly="readonly"/>readonly="readonly"/>
</form></form>
</body></body>
</html></html>
Form validation
<html><head>
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false; }}
</script> </head>
<body>
<form name="myForm" action="/action_page.php" onsubmit="return
validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body></html>
02/02/19 20
02/02/19 21

More Related Content

PPT
Vbscript
PDF
PPT
vb script
PPT
Vb script
PPTX
Introduction To JavaScript
PPT
Java script
PDF
Vb script tutorial for qtp[1]
PDF
Introduction to Javascript programming
Vbscript
vb script
Vb script
Introduction To JavaScript
Java script
Vb script tutorial for qtp[1]
Introduction to Javascript programming

What's hot (20)

PPTX
Java Script
DOC
Basics java scripts
PPTX
Introduction to java script
PPT
Web development basics (Part-4)
RTF
Java scripts
PPTX
Introduction to Javascript By Satyen
DOC
Introduction to java script
PPTX
Introduction to JavaScript
PPT
Java script
PPTX
Javascript
PPTX
Java script writing javascript
PDF
10 Groovy Little JavaScript Tips
PPTX
1. java script language fundamentals
PPTX
JavaScript Core fundamentals - Learn JavaScript Here
PPTX
Server Scripting Language -PHP
PPT
JavaScript
PPT
Javascript Basics
PDF
Seaside - Web Development As You Like It
PPTX
Java script
PPTX
JavaScript Basics
Java Script
Basics java scripts
Introduction to java script
Web development basics (Part-4)
Java scripts
Introduction to Javascript By Satyen
Introduction to java script
Introduction to JavaScript
Java script
Javascript
Java script writing javascript
10 Groovy Little JavaScript Tips
1. java script language fundamentals
JavaScript Core fundamentals - Learn JavaScript Here
Server Scripting Language -PHP
JavaScript
Javascript Basics
Seaside - Web Development As You Like It
Java script
JavaScript Basics
Ad

Similar to Js mod1 (20)

PDF
WT UNIT 2 presentation :client side technologies JavaScript And Dom
ODP
JavaScript and jQuery Fundamentals
PPT
JavaScript
PDF
Wt unit 2 ppts client side technology
PDF
Wt unit 2 ppts client sied technology
PPT
FSJavaScript.ppt
PPT
JavaScript
PPTX
Java script
PPTX
BITM3730 10-17.pptx
PPTX
Unit III.pptx IT3401 web essentials presentatio
PPT
Javascript1
PPTX
Basics of Java Script (JS)
PPT
JavaScript Training
PPTX
Javascript
PDF
2013-06-25 - HTML5 & JavaScript Security
PDF
Jscript Fundamentals
PPSX
Javascript variables and datatypes
PDF
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
PPT
Java script
PPTX
Java script
WT UNIT 2 presentation :client side technologies JavaScript And Dom
JavaScript and jQuery Fundamentals
JavaScript
Wt unit 2 ppts client side technology
Wt unit 2 ppts client sied technology
FSJavaScript.ppt
JavaScript
Java script
BITM3730 10-17.pptx
Unit III.pptx IT3401 web essentials presentatio
Javascript1
Basics of Java Script (JS)
JavaScript Training
Javascript
2013-06-25 - HTML5 & JavaScript Security
Jscript Fundamentals
Javascript variables and datatypes
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
Java script
Java script
Ad

More from VARSHAKUMARI49 (16)

PPT
28,29. procedures subprocedure,type checking functions in VBScript
PPT
30,31,32,33. decision and loop statements in vbscript
PPT
27. mathematical, date and time functions in VB Script
PPT
Cascading style sheet
PPT
Introduction to web technology
PPT
Database normalization
PPT
PPT
Sub queries
PPT
Introduction to sql
PPTX
Css module1
PPTX
Css mod1
PPT
Html mod1
PPT
Register counters.readonly
PPT
Sorting.ppt read only
PPT
28,29. procedures subprocedure,type checking functions in VBScript
30,31,32,33. decision and loop statements in vbscript
27. mathematical, date and time functions in VB Script
Cascading style sheet
Introduction to web technology
Database normalization
Sub queries
Introduction to sql
Css module1
Css mod1
Html mod1
Register counters.readonly
Sorting.ppt read only

Recently uploaded (20)

PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Mechanical Engineering MATERIALS Selection
PDF
Digital Logic Computer Design lecture notes
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Welding lecture in detail for understanding
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
additive manufacturing of ss316l using mig welding
PDF
PPT on Performance Review to get promotions
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Model Code of Practice - Construction Work - 21102022 .pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mechanical Engineering MATERIALS Selection
Digital Logic Computer Design lecture notes
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Lesson 3_Tessellation.pptx finite Mathematics
Arduino robotics embedded978-1-4302-3184-4.pdf
Welding lecture in detail for understanding
Internet of Things (IOT) - A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Lecture Notes Electrical Wiring System Components
additive manufacturing of ss316l using mig welding
PPT on Performance Review to get promotions
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Embodied AI: Ushering in the Next Era of Intelligent Systems

Js mod1

  • 2. Introduction • JavaScript is a front-end scripting language developed by Netscape for dynamic content – Lightweight, but with limited capabilities – Can be used as object-oriented language • Client-side technology – Embedded in your HTML page – Interpreted by the Web browser • Simple and flexible 02/02/19 2
  • 3. • HTML to define the content of web pages • CSS to specify the layout of web pages • JavaScript to program the dynamic behavior of web pages 02/02/19 3
  • 4. Using JavaScript Code • The JavaScript code can be placed in: – <script> tag in the head </script> – <script> tag in the body </script> – External files, linked via <script> tag in the head • Files usually have .js extension 4 <script src="scripts.js" type="text/javscript"><script src="scripts.js" type="text/javscript"> </script></script>
  • 5. The First Script first-script.html 5 <html><html> <body><body> <script type="text/javascript"><script type="text/javascript"> alert('Hello JavaScript!');alert('Hello JavaScript!'); </script></script> </body></body> </html></html>
  • 6. Another Small Example small-example.html 6 <html><html> <body><body> <script type="text/javascript"><script type="text/javascript"> document.write('JavaScript rulez!');document.write('JavaScript rulez!'); </script></script> </body></body> </html></html>
  • 7. <html><html> <head><head> <script type="text/javascript"><script type="text/javascript"> function test (message) {function test (message) { alert(message);alert(message); }} </script></script> </head></head> <body><body> <img src="logo.gif"<img src="logo.gif" onclick="test('clicked!')" />onclick="test('clicked!')" /> </body></body> </html></html> Calling a JavaScript Function from Event Handler – Example image-onclick.html 7
  • 8. Using External Script Files• Using external script files: External JavaScript file: 8 <html><html> <head><head> <script src="sample.js" type="text/javascript"><script src="sample.js" type="text/javascript"> </script></script> </head></head> <body><body> <button onclick="sample()" value="Call JavaScript<button onclick="sample()" value="Call JavaScript function from sample.js" />function from sample.js" /> </body></body> </html></html> function sample() {function sample() { alert('Hello from sample.js!')alert('Hello from sample.js!') external-JavaScript.htmlexternal-JavaScript.html sample.jssample.js The <script> tag is always empty.The <script> tag is always empty.
  • 9. <script type = "text/JavaScript" language="JavaScript"> var a = "These sentence"; var italics_a = a.italics(); var b = "will continue over"; var upper_b = b.toUpperCase(); var c = "three variables"; var red_c = c.fontcolor("red"); var sentence = a+b+c; var sentence_change = italics_a+upper_b+red_c; document.write(sentence); document.write("<br>"); document.write(sentence_change); </script>
  • 10. Standard Popup Boxes • Alert box with text and [OK] button – Just a message shown in a dialog box: • Confirmation box – Contains text, [OK] button and [Cancel] button: • Prompt box – Contains text, input field with default value: 10 alert("Some text here");alert("Some text here"); confirm("Are you sure?");confirm("Are you sure?"); prompt ("enter amount", 10);prompt ("enter amount", 10);
  • 11. Alert Dialog Box <html> <head> <script type="text/javascript"> function Warn() { alert ("This is a warning message!"); document.write ("This is a warning message!"); } </script> </head> <body> <p>Click the following button to see the result: </p> <form> <input type="button" value="Click Me" onclick="Warn();" /> </form> </body></html>
  • 12. Confirmation Dialog Box <html> <head> <script type="text/javascript"> function getConfirmation() { var retVal = confirm("Do you want to continue ?"); if( retVal == true ) { document.write ("User wants to continue!"); return true; } Else { document.write ("User does not want to continue!"); return false; } } </script> </head> <body> <p>Click the following button to see the result: </p> <form> <input type="button" value="Click Me" onclick="getConfirmation();" /> </form> </body> </html>
  • 13. Prompt Dialog Box <html> <head> <script type="text/javascript"> function getValue(){ var retVal = prompt("Enter your name : ", "your name here"); document.write("You have entered : " + retVal); } </script> </head> <body> <p>Click the following button to see the result: </p> <form> <input type="button" value="Click Me" onclick="getValue();" /> </form> </body></html>
  • 14. JavaScript Syntax • The JavaScript syntax is similar to Java – Operators (+, *, =, !=, &&, ++, …) – Variables (typeless) – Conditional statements (if, else) – Loops (for, while) – Arrays (my_array[]) and associative arrays (my_array['abc']) – Functions (can return value) 14
  • 15. Data Types • JavaScript data types: – Numbers (integer, floating-point) – Boolean (true / false) • String type – string of characters • Arrays • Associative arrays (hash tables) 15 var myName = "You can use both single or doublevar myName = "You can use both single or double quotes for strings";quotes for strings"; var my_array = [1, 5.3, "aaa"];var my_array = [1, 5.3, "aaa"]; var my_hash = {a:2, b:3, c:"text"};var my_hash = {a:2, b:3, c:"text"};
  • 16. Everything is Object• Every variable can be considered as object – For example strings and arrays have member functions: 16 var test = "some string";var test = "some string"; alert(test[7]); // shows letter 'r'alert(test[7]); // shows letter 'r' alert(test.charAt(5)); // shows letter 's'alert(test.charAt(5)); // shows letter 's' alert("test".charAt(1)); //shows letter 'e'alert("test".charAt(1)); //shows letter 'e' alert("test".substring(1,3)); //shows 'esalert("test".substring(1,3)); //shows 'es'' var arr = [1,3,4];var arr = [1,3,4]; alert (arr.length); // shows 3alert (arr.length); // shows 3 arr.push(7); // appends 7 to end of arrayarr.push(7); // appends 7 to end of array alert (arr[3]); // shows 7alert (arr[3]); // shows 7 objects.htmlobjects.html
  • 17. String Operations The + operator joins strings • What is "9" + 9? • Converting string to number: 17 string1 = "fat ";string1 = "fat "; string2 = "cats";string2 = "cats"; alert(string1 + string2); // fat catsalert(string1 + string2); // fat cats alert("9" + 9); // 99alert("9" + 9); // 99 alert(parseInt("9") + 9); // 18alert(parseInt("9") + 9); // 18
  • 18. Sum of Numbers – Example sum-of-numbers.html 18 <html><html> <head><head> <title>JavaScript Demo</title><title>JavaScript Demo</title> <script type="text/javascript"><script type="text/javascript"> function calcSum() {function calcSum() { value1 =value1 = parseInt(document.mainForm.textBox1.value);parseInt(document.mainForm.textBox1.value); value2 =value2 = parseInt(document.mainForm.textBox2.value);parseInt(document.mainForm.textBox2.value); sum = value1 + value2;sum = value1 + value2; document.mainForm.textBoxSum.value = sum;document.mainForm.textBoxSum.value = sum; }} </script></script> </head></head>
  • 19. Sum of Numbers – Example (2) sum-of-numbers.html (cont.) 19 <body><body> <form name="mainForm"><form name="mainForm"> <input type="text" name="textBox1" /> <br/><input type="text" name="textBox1" /> <br/> <input type="text" name="textBox2" /> <br/><input type="text" name="textBox2" /> <br/> <input type="button" value="Process"<input type="button" value="Process" onclick="javascript: calcSum()" />onclick="javascript: calcSum()" /> <input type="text" name="textBoxSum"<input type="text" name="textBoxSum" readonly="readonly"/>readonly="readonly"/> </form></form> </body></body> </html></html>
  • 20. Form validation <html><head> <script> function validateForm() { var x = document.forms["myForm"]["fname"].value; if (x == "") { alert("Name must be filled out"); return false; }} </script> </head> <body> <form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post"> Name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> </body></html> 02/02/19 20