SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
JavaScript
 JavaScript – programming language that can
 appear in html pages.
 It allow us to:
   To dynamically create web pages
   To control a browser application
     Open and create new browser windows
     Download and display contents of any URL
   To interact with the user
   Ability to interact with HTML forms
     You can process values provided by checkbox, text, textarea
     buttons




                                                                   1
JavaScript
 What is not possible with JavaScript
   It is not possible to read and write files (security reasons)
   The only networking support it provides is:
      It can send the contents of forms to a server and e-mail
      addresses
      It can cause the browser to load a web page
 JavaScript is not Java, however …
   JavaScript program constructs are similar to Java’s
   constructs (in many cases identical)
   JavaScript can interact with java programs




                                                                   2
JavaScript
JavaScript Interpreter – Process javaScript code
To write JavaScript programs you need
   A web browser
   A text editor
A JavaScript program can appear
   In a file by itself typically named with the extension .js
   In html files between a <script> and </script> tags
Client-Side JavaScript – the result of embedding a JavaScript
interpreter in a web browser
Example1 (See Example1.html)
   We use document.writeln to create the page contents
   Notice the html tags present in the writeln




                                                                3
JavaScript
When learning JavaScript we can identify two main areas
   Core JavaScript – Set of rules specifying how to write JavaScript
   programs
   Client-Side JavaScript – How JavaScript is used in web browsers
A JavaScript program can appear
   In a file by itself in a file typically with the extension .js
   In html files between a <script> and </script> tags
Example1
   We use document.writeln to create the page contents
   Notice the html tags present in the writeln




                                                                       4
Embedding JavaScript in HTML
Different ways to embed JavaScript in HTML
  By using <script> and </script> tags in the html document
  From an external file (specified via URL) using the src
  attribute of the script tag
    <script src=“demo.js”></script>
    script src behaves as if contents of file appears directly
    between the tags
  As event handler
    Example2 (See Example2.html)
  JavaScript URLs
    javascript:alert(“Welcome”)




                                                                 5
<script> Embedding
You may place any number of JavaScript statements
between <script> and </script>
Statements are executed as the document is loaded
<script> may appear in the head or body section of an
html document
Several pairs of nonoverlapping <script></script> blocks
can appear in a document
  All are consider part of the same program
  If you define a value in a block it can be referred from another
  block




                                                                     6
<script> Embedding
language attribute (e.g., <script language=“JavaScript”></script>)
   JavaScript is not the only language out there
      Visual Basic Scripting Language (language = “VBScript”)
   IE and Netscape will assume JavaScript if language is not specified.
   The language attribute allows you to specify a JavaScript version to use
   (e.g., <script language=“JavaScript1.5”>
   If a browser doesn’t understand a language it will ignore the statements
type (e.g, <script type=“text/javascript”>
   HTML 4 deprecates the language attribute (although language is widely
   use) using instead the type attribute




                                                                              7
External File Embedding
Script src behaves as if contents of file appears directly
between the tags
Example3 (See Example3.html and footer.js)
The src specifies an URL as its value
Advantages of external file
  Simplifies html files by removing code file
  Allows you to share code among html files
  Improves efficiency by allowing the web browser to
  cache the code




                                                             8
Event Handlers Embedding
Dynamic interaction with a user is possible by
responding to events (e.g., when the user clicks on a
button)
Events – Originate from html objects (e.g. buttons)
Event Handler – What will take place when an Event
occurs
Event Handler defined as attribute of html object
  In Example 2 the handler is represented by the string (collection
  of characters) that follows the onclick
  alert(new Date()) represents a statement. Each statement must
  be separated by semicolons.
  When someone selects the button the code is executed.




                                                                      9
Execution of JavaScript
Programs
 HTML parser – Takes care of processing an html
 document
 JavaScript interpreter – Takes care of processing
 JavaScript code
 HTML parser – must stop processing an html file when
 JavaScript code is found (JavaScript interpreter will then
 be running)
   This implies a page with JavaScript code that is computationally
   intensive can take a long time to load




                                                                  10
JavaScript
 Let’s go over several basic constructs that allow us to define
 JavaScript programs.
 Some definitions
    string – Any set of characters in double quotes (“ “)
    function/method – An entity that completes a particular task for
    us. It can takes values necessary to complete the particular task
    and it can return values.
 Generating Output with the document.writeln method
    Allow us to add text to the html file (see Example1) by providing
    the required text in “ “
    You can specify html code and results of JavaScript constructs




                                                                    11
JavaScript (Output)
 Example 4 (See Example4.html)
   Illustrates how we can create a table using document.writeln
   Notice how we can use the Date() to specify a particular date
   format. Date() is part of JavaScript
   The + allow us to concatenate strings
   Notice how we have specified the border size. If you use “ “ then
   the table will not be generated. You need to use single quotes.
   Keep in mind that this example could have been written without
   using JavaScript. However you will see how by extending code
   similar to the one provided you can dynamically decide what your
   final html look like




                                                                   12
JavaScript (Variables )
 variable – A memory location that can store a value. In JavaScript
 variables are declared using var
     var name;
 Variables names must start with a letter and can be followed by
 letters and digits
 A variable can hold different type of values
 Values we can assign to variables
     Integer – 0, 10, 40, 6, -7
     Floating-point – 3.2, .67, 1.48E-20
     String literals – “hello”, “goodbye”
 Operators
     Typical arithmetic operators (+, -, *, /)
 Example 5 (See Example5.html)


                                                                      13
JavaScript (Dialog Boxes)
 We can perform input and output via dialog boxes
 Input via prompt, Example 6 (See Example6.html)
   Notice we can define several variables at the same
   time
 Comments
   Can be defined using // (Everything until end of the
   line is a comment)
   Can be defined using /* */ (Everything in between /*
   and */ is considered a comment.
   /* */ can span several lines (// don’t)



                                                          14
JavaScript References

  JavaScript The Definitive Guide by David
  Flanagan
  ISBN: 0-596-00048-0

  JavaScript Pocket Reference by David Flanagan
  ISBN: 0-596-00411-7




                                                  15

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Javascript
JavascriptJavascript
Javascript
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
Web programming
Web programmingWeb programming
Web programming
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
Introduction to Javascript By Satyen
Introduction to Javascript By  SatyenIntroduction to Javascript By  Satyen
Introduction to Javascript By Satyen
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Carlos Amador .Net Portfolio
Carlos Amador .Net PortfolioCarlos Amador .Net Portfolio
Carlos Amador .Net Portfolio
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & Implementation
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
 
Basics java scripts
Basics java scriptsBasics java scripts
Basics java scripts
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Java scripts
Java scriptsJava scripts
Java scripts
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
Java script
Java scriptJava script
Java script
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 

Semelhante a Lect35 javascript

Session vii(java scriptbasics)
Session vii(java scriptbasics)Session vii(java scriptbasics)
Session vii(java scriptbasics)Shrijan Tiwari
 
JavaScript: Implementations And Applications
JavaScript: Implementations And ApplicationsJavaScript: Implementations And Applications
JavaScript: Implementations And ApplicationsPragya Pai
 
CHAPTER 3 JS (1).pptx
CHAPTER 3  JS (1).pptxCHAPTER 3  JS (1).pptx
CHAPTER 3 JS (1).pptxachutachut
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPTGo4Guru
 
Basic Java script handouts for students
Basic Java script handouts for students Basic Java script handouts for students
Basic Java script handouts for students shafiq sangi
 
Java Script - A New Look
Java Script - A New LookJava Script - A New Look
Java Script - A New Lookrumsan
 
8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentationJohnLagman3
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java scriptnanjil1984
 
M6 l7-java script-handout
M6 l7-java script-handoutM6 l7-java script-handout
M6 l7-java script-handoutNolboo Kim
 
Java script by Act Academy
Java script by Act AcademyJava script by Act Academy
Java script by Act Academyactanimation
 
Java script Basic
Java script BasicJava script Basic
Java script BasicJaya Kumari
 
Introduction to Javascript programming
Introduction to Javascript programmingIntroduction to Javascript programming
Introduction to Javascript programmingFulvio Corno
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGArulkumar
 

Semelhante a Lect35 javascript (20)

Session vii(java scriptbasics)
Session vii(java scriptbasics)Session vii(java scriptbasics)
Session vii(java scriptbasics)
 
CSC PPT 12.pptx
CSC PPT 12.pptxCSC PPT 12.pptx
CSC PPT 12.pptx
 
JavaScript: Implementations And Applications
JavaScript: Implementations And ApplicationsJavaScript: Implementations And Applications
JavaScript: Implementations And Applications
 
Javascript tutorial
Javascript tutorialJavascript tutorial
Javascript tutorial
 
CHAPTER 3 JS (1).pptx
CHAPTER 3  JS (1).pptxCHAPTER 3  JS (1).pptx
CHAPTER 3 JS (1).pptx
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
Lecture-15.pptx
Lecture-15.pptxLecture-15.pptx
Lecture-15.pptx
 
JS basics
JS basicsJS basics
JS basics
 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
 
Basic Java script handouts for students
Basic Java script handouts for students Basic Java script handouts for students
Basic Java script handouts for students
 
Java Script
Java ScriptJava Script
Java Script
 
Java Script - A New Look
Java Script - A New LookJava Script - A New Look
Java Script - A New Look
 
JavaScript
JavaScriptJavaScript
JavaScript
 
8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation8.-Javascript-report powerpoint presentation
8.-Javascript-report powerpoint presentation
 
Introduction to java script
Introduction to java scriptIntroduction to java script
Introduction to java script
 
M6 l7-java script-handout
M6 l7-java script-handoutM6 l7-java script-handout
M6 l7-java script-handout
 
Java script by Act Academy
Java script by Act AcademyJava script by Act Academy
Java script by Act Academy
 
Java script Basic
Java script BasicJava script Basic
Java script Basic
 
Introduction to Javascript programming
Introduction to Javascript programmingIntroduction to Javascript programming
Introduction to Javascript programming
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
 

Último

/:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc...
/:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc.../:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc...
/:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc...lizamodels9
 
Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...
Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...
Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...lizamodels9
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Dipal Arora
 
7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...Paul Menig
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Timedelhimodelshub1
 
Marketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet CreationsMarketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet Creationsnakalysalcedo61
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Catalogue ONG NUOC PPR DE NHAT .pdf
Catalogue ONG NUOC PPR DE NHAT      .pdfCatalogue ONG NUOC PPR DE NHAT      .pdf
Catalogue ONG NUOC PPR DE NHAT .pdfOrient Homes
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
Tech Startup Growth Hacking 101  - Basics on Growth MarketingTech Startup Growth Hacking 101  - Basics on Growth Marketing
Tech Startup Growth Hacking 101 - Basics on Growth MarketingShawn Pang
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni
 
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Howrah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Roomdivyansh0kumar0
 
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...lizamodels9
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfpollardmorgan
 
Pitch Deck Teardown: NOQX's $200k Pre-seed deck
Pitch Deck Teardown: NOQX's $200k Pre-seed deckPitch Deck Teardown: NOQX's $200k Pre-seed deck
Pitch Deck Teardown: NOQX's $200k Pre-seed deckHajeJanKamps
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewasmakika9823
 

Último (20)

/:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc...
/:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc.../:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc...
/:Call Girls In Jaypee Siddharth - 5 Star Hotel New Delhi ➥9990211544 Top Esc...
 
Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...
Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...
Lowrate Call Girls In Laxmi Nagar Delhi ❤️8860477959 Escorts 100% Genuine Ser...
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 
7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...7.pdf This presentation captures many uses and the significance of the number...
7.pdf This presentation captures many uses and the significance of the number...
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Time
 
Marketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet CreationsMarketing Management Business Plan_My Sweet Creations
Marketing Management Business Plan_My Sweet Creations
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Catalogue ONG NUOC PPR DE NHAT .pdf
Catalogue ONG NUOC PPR DE NHAT      .pdfCatalogue ONG NUOC PPR DE NHAT      .pdf
Catalogue ONG NUOC PPR DE NHAT .pdf
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
Tech Startup Growth Hacking 101  - Basics on Growth MarketingTech Startup Growth Hacking 101  - Basics on Growth Marketing
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.
 
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Howrah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
 
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
 
Pitch Deck Teardown: NOQX's $200k Pre-seed deck
Pitch Deck Teardown: NOQX's $200k Pre-seed deckPitch Deck Teardown: NOQX's $200k Pre-seed deck
Pitch Deck Teardown: NOQX's $200k Pre-seed deck
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
 
KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)
 

Lect35 javascript

  • 1. JavaScript JavaScript – programming language that can appear in html pages. It allow us to: To dynamically create web pages To control a browser application Open and create new browser windows Download and display contents of any URL To interact with the user Ability to interact with HTML forms You can process values provided by checkbox, text, textarea buttons 1
  • 2. JavaScript What is not possible with JavaScript It is not possible to read and write files (security reasons) The only networking support it provides is: It can send the contents of forms to a server and e-mail addresses It can cause the browser to load a web page JavaScript is not Java, however … JavaScript program constructs are similar to Java’s constructs (in many cases identical) JavaScript can interact with java programs 2
  • 3. JavaScript JavaScript Interpreter – Process javaScript code To write JavaScript programs you need A web browser A text editor A JavaScript program can appear In a file by itself typically named with the extension .js In html files between a <script> and </script> tags Client-Side JavaScript – the result of embedding a JavaScript interpreter in a web browser Example1 (See Example1.html) We use document.writeln to create the page contents Notice the html tags present in the writeln 3
  • 4. JavaScript When learning JavaScript we can identify two main areas Core JavaScript – Set of rules specifying how to write JavaScript programs Client-Side JavaScript – How JavaScript is used in web browsers A JavaScript program can appear In a file by itself in a file typically with the extension .js In html files between a <script> and </script> tags Example1 We use document.writeln to create the page contents Notice the html tags present in the writeln 4
  • 5. Embedding JavaScript in HTML Different ways to embed JavaScript in HTML By using <script> and </script> tags in the html document From an external file (specified via URL) using the src attribute of the script tag <script src=“demo.js”></script> script src behaves as if contents of file appears directly between the tags As event handler Example2 (See Example2.html) JavaScript URLs javascript:alert(“Welcome”) 5
  • 6. <script> Embedding You may place any number of JavaScript statements between <script> and </script> Statements are executed as the document is loaded <script> may appear in the head or body section of an html document Several pairs of nonoverlapping <script></script> blocks can appear in a document All are consider part of the same program If you define a value in a block it can be referred from another block 6
  • 7. <script> Embedding language attribute (e.g., <script language=“JavaScript”></script>) JavaScript is not the only language out there Visual Basic Scripting Language (language = “VBScript”) IE and Netscape will assume JavaScript if language is not specified. The language attribute allows you to specify a JavaScript version to use (e.g., <script language=“JavaScript1.5”> If a browser doesn’t understand a language it will ignore the statements type (e.g, <script type=“text/javascript”> HTML 4 deprecates the language attribute (although language is widely use) using instead the type attribute 7
  • 8. External File Embedding Script src behaves as if contents of file appears directly between the tags Example3 (See Example3.html and footer.js) The src specifies an URL as its value Advantages of external file Simplifies html files by removing code file Allows you to share code among html files Improves efficiency by allowing the web browser to cache the code 8
  • 9. Event Handlers Embedding Dynamic interaction with a user is possible by responding to events (e.g., when the user clicks on a button) Events – Originate from html objects (e.g. buttons) Event Handler – What will take place when an Event occurs Event Handler defined as attribute of html object In Example 2 the handler is represented by the string (collection of characters) that follows the onclick alert(new Date()) represents a statement. Each statement must be separated by semicolons. When someone selects the button the code is executed. 9
  • 10. Execution of JavaScript Programs HTML parser – Takes care of processing an html document JavaScript interpreter – Takes care of processing JavaScript code HTML parser – must stop processing an html file when JavaScript code is found (JavaScript interpreter will then be running) This implies a page with JavaScript code that is computationally intensive can take a long time to load 10
  • 11. JavaScript Let’s go over several basic constructs that allow us to define JavaScript programs. Some definitions string – Any set of characters in double quotes (“ “) function/method – An entity that completes a particular task for us. It can takes values necessary to complete the particular task and it can return values. Generating Output with the document.writeln method Allow us to add text to the html file (see Example1) by providing the required text in “ “ You can specify html code and results of JavaScript constructs 11
  • 12. JavaScript (Output) Example 4 (See Example4.html) Illustrates how we can create a table using document.writeln Notice how we can use the Date() to specify a particular date format. Date() is part of JavaScript The + allow us to concatenate strings Notice how we have specified the border size. If you use “ “ then the table will not be generated. You need to use single quotes. Keep in mind that this example could have been written without using JavaScript. However you will see how by extending code similar to the one provided you can dynamically decide what your final html look like 12
  • 13. JavaScript (Variables ) variable – A memory location that can store a value. In JavaScript variables are declared using var var name; Variables names must start with a letter and can be followed by letters and digits A variable can hold different type of values Values we can assign to variables Integer – 0, 10, 40, 6, -7 Floating-point – 3.2, .67, 1.48E-20 String literals – “hello”, “goodbye” Operators Typical arithmetic operators (+, -, *, /) Example 5 (See Example5.html) 13
  • 14. JavaScript (Dialog Boxes) We can perform input and output via dialog boxes Input via prompt, Example 6 (See Example6.html) Notice we can define several variables at the same time Comments Can be defined using // (Everything until end of the line is a comment) Can be defined using /* */ (Everything in between /* and */ is considered a comment. /* */ can span several lines (// don’t) 14
  • 15. JavaScript References JavaScript The Definitive Guide by David Flanagan ISBN: 0-596-00048-0 JavaScript Pocket Reference by David Flanagan ISBN: 0-596-00411-7 15