SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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

Weitere ähnliche Inhalte

Was ist angesagt?

Basics java scripts
Basics java scriptsBasics java scripts
Basics java scriptsch samaram
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java scriptDivyaKS12
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)Rajat Pratap Singh
 
Introduction to Javascript By Satyen
Introduction to Javascript By  SatyenIntroduction to Javascript By  Satyen
Introduction to Javascript By SatyenSatyen Pandya
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java scriptnanjil1984
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMarlon Jamera
 
Java script
Java scriptJava script
Java scriptITz_1
 
Java script writing javascript
Java script writing javascriptJava script writing javascript
Java script writing javascriptJesus Obenita Jr.
 
10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript TipsTroy Miles
 
1. java script language fundamentals
1. java script language fundamentals1. java script language fundamentals
1. java script language fundamentalsRajiv Gupta
 
JavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereJavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereLaurence Svekis ✔
 
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHPDeo Shao
 
Seaside - Web Development As You Like It
Seaside - Web Development As You Like ItSeaside - Web Development As You Like It
Seaside - Web Development As You Like ItLukas Renggli
 

Was ist angesagt? (20)

Java Script
Java ScriptJava Script
Java Script
 
Basics java scripts
Basics java scriptsBasics java scripts
Basics java scripts
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
 
Java scripts
Java scriptsJava scripts
Java scripts
 
Introduction to Javascript By Satyen
Introduction to Javascript By  SatyenIntroduction to Javascript By  Satyen
Introduction to Javascript By Satyen
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Java script
Java scriptJava script
Java script
 
Javascript
JavascriptJavascript
Javascript
 
Java script writing javascript
Java script writing javascriptJava script writing javascript
Java script writing javascript
 
10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips
 
1. java script language fundamentals
1. java script language fundamentals1. java script language fundamentals
1. java script language fundamentals
 
JavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereJavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript Here
 
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHP
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Seaside - Web Development As You Like It
Seaside - Web Development As You Like ItSeaside - Web Development As You Like It
Seaside - Web Development As You Like It
 
Java script
Java scriptJava script
Java script
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 

Ähnlich wie Js mod1 (20)

JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
JavaScript
JavaScriptJavaScript
JavaScript
 
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
 
FSJavaScript.ppt
FSJavaScript.pptFSJavaScript.ppt
FSJavaScript.ppt
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Java script
Java scriptJava script
Java script
 
BITM3730 10-17.pptx
BITM3730 10-17.pptxBITM3730 10-17.pptx
BITM3730 10-17.pptx
 
Javascript1
Javascript1Javascript1
Javascript1
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
JavaScript Training
JavaScript TrainingJavaScript Training
JavaScript Training
 
Javascript
JavascriptJavascript
Javascript
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentals
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
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
 
Java script
Java scriptJava script
Java script
 
Java script
 Java script Java script
Java script
 
Java script
Java scriptJava script
Java script
 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
 

Mehr von VARSHAKUMARI49

28,29. procedures subprocedure,type checking functions in VBScript
28,29. procedures  subprocedure,type checking functions in VBScript28,29. procedures  subprocedure,type checking functions in VBScript
28,29. procedures subprocedure,type checking functions in VBScriptVARSHAKUMARI49
 
30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscriptVARSHAKUMARI49
 
27. mathematical, date and time functions in VB Script
27. mathematical, date and time  functions in VB Script27. mathematical, date and time  functions in VB Script
27. mathematical, date and time functions in VB ScriptVARSHAKUMARI49
 
Introduction to web technology
Introduction to web technologyIntroduction to web technology
Introduction to web technologyVARSHAKUMARI49
 
Database normalization
Database normalizationDatabase normalization
Database normalizationVARSHAKUMARI49
 
Register counters.readonly
Register counters.readonlyRegister counters.readonly
Register counters.readonlyVARSHAKUMARI49
 

Mehr von VARSHAKUMARI49 (16)

28,29. procedures subprocedure,type checking functions in VBScript
28,29. procedures  subprocedure,type checking functions in VBScript28,29. procedures  subprocedure,type checking functions in VBScript
28,29. procedures subprocedure,type checking functions in VBScript
 
30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript
 
27. mathematical, date and time functions in VB Script
27. mathematical, date and time  functions in VB Script27. mathematical, date and time  functions in VB Script
27. mathematical, date and time functions in VB Script
 
Cascading style sheet
Cascading style sheetCascading style sheet
Cascading style sheet
 
Html
HtmlHtml
Html
 
Introduction to web technology
Introduction to web technologyIntroduction to web technology
Introduction to web technology
 
Database normalization
Database normalizationDatabase normalization
Database normalization
 
Joins
JoinsJoins
Joins
 
Sub queries
Sub queriesSub queries
Sub queries
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Css module1
Css module1Css module1
Css module1
 
Css mod1
Css mod1Css mod1
Css mod1
 
Html mod1
Html mod1Html mod1
Html mod1
 
Register counters.readonly
Register counters.readonlyRegister counters.readonly
Register counters.readonly
 
Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
 
Hashing
HashingHashing
Hashing
 

Kürzlich hochgeladen

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringJuanCarlosMorales19600
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 

Kürzlich hochgeladen (20)

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineering
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 

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