SlideShare a Scribd company logo
1 of 16
Introduction to JavaScript
Introduction
 What is it?
 How does it work?
 What is Java?
 Learning JavaScript
 JavaScript Statements
 JavaScript and HTML forms
What is JavaScript?
 Browsers have limited functionality
 Text, images, tables, frames
 JavaScript allows for interactivity
 Browser/page manipulation
 Reacting to user actions
 A type of programming language
 Easy to learn
 Developed by Netscape
 Now a standard exists –
www.ecma-international.org/publications/
standards/ECMA-262.HTM
JavaScript Allows Interactivity
 Improve appearance
 Especially graphics
 Visual feedback
 Site navigation
 Perform calculations
 Validation of input
 Other technologies
javascript.internet.com
How Does It Work?
 Embedded within HTML page
 View source
 Executes on client
 Fast, no connection needed once loaded
 Simple programming statements combined with
HTML tags
 Interpreted (not compiled)
 No special tools required
What is Java?
 Totally different
 A full programming language
 Much harder!
 A compiled language
 Independent of the web
 Sometimes used together
Learning JavaScript
 Special syntax to learn
 Learn the basics and then use other people's
(lots of free sites)
 Write it in a text editor, view results in browser
 You need to revise your HTML
 You need patience and good eyesight!
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language="JavaScript">
document.write('This is my first 
JavaScript Page');
</script>
</body>
</html>
Note the symbol for
line continuation
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language=“JavaScript">
document.write('<h1>This is my first 
JavaScript Page</h1>');
</script>
</body>
</html>
HTML written
inside JavaScript
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<p>
<a href="myfile.html">My Page</a>
<br />
<a href="myfile.html"
onMouseover="window.alert('Hello');">
My Page</A>
</p>
</body>
</html>
JavaScript written
inside HTML
An Event
Example Statements
<script language="JavaScript">
window.prompt('Enter your name:','');
</script>
<form>
<input type="button" Value="Press"
onClick="window.alert('Hello');">
</form>
Another event
Note quotes: " and '
HTML Forms and JavaScript
 JavaScript is very good at processing user input
in the web browser
 HTML <form> elements receive input
 Forms and form elements have unique names
 Each unique element can be identified
 Uses JavaScript Document Object Model (DOM)
Naming Form Elements in HTML
<form name="addressform">
Name: <input name="yourname"><br />
Phone: <input name="phone"><br />
Email: <input name="email"><br />
</form>
Forms and JavaScript
document.formname.elementname.value
Thus:
document.addressform.yourname.value
document.addressform.phone.value
document.addressform.email.value
Using Form Data
Personalising an alert box
<form name="alertform">
Enter your name:
<input type="text" name="yourname">
<input type="button" value= "Go"
onClick="window.alert('Hello ' + 
document.alertform.yourname.value);">
</form>
Tips
 Check your statements are on one line
 Check your " and ' quotes match
 Take care with capitalisation
 Lay it out neatly - use tabs
 Remember  in the workbook denotes a
continuing line
 Be patient
Presentation slides adapted from scom.hud.ac.uk/scomsjw/Web%20Authoring%20Module/Lecture%20Slides/introjs.ppt

More Related Content

Similar to Introduction to JavaScript

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorial
tutorialsruby
 
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
tutorialsruby
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorial
tutorialsruby
 
Java script writing javascript
Java script writing javascriptJava script writing javascript
Java script writing javascript
Jesus Obenita Jr.
 
Basics java scripts
Basics java scriptsBasics java scripts
Basics java scripts
ch samaram
 

Similar to Introduction to JavaScript (20)

JavaScriptL18 [Autosaved].pptx
JavaScriptL18 [Autosaved].pptxJavaScriptL18 [Autosaved].pptx
JavaScriptL18 [Autosaved].pptx
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorial
 
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
Essential Javascript -- A Javascript &lt;b>Tutorial&lt;/b>
 
Essential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_TutorialEssential_Javascript_--_A_Javascript_Tutorial
Essential_Javascript_--_A_Javascript_Tutorial
 
Html intro
Html introHtml intro
Html intro
 
Java script writing javascript
Java script writing javascriptJava script writing javascript
Java script writing javascript
 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
 
Decoding the Web
Decoding the WebDecoding the Web
Decoding the Web
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
 
Day1
Day1Day1
Day1
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
 
Java script
Java scriptJava script
Java script
 
Javascript
JavascriptJavascript
Javascript
 
Introduction to Java Script
Introduction to Java ScriptIntroduction to Java Script
Introduction to Java Script
 
Basics java scripts
Basics java scriptsBasics java scripts
Basics java scripts
 
Intro to mobile web application development
Intro to mobile web application developmentIntro to mobile web application development
Intro to mobile web application development
 
Jsp
JspJsp
Jsp
 
Web Designing
Web DesigningWeb Designing
Web Designing
 

Recently uploaded

Recently uploaded (20)

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Introduction to JavaScript

Editor's Notes

  1. This talk introduces the JavaScript language. © Netskills, University of Newcastle Copyright in the whole and every part of this Courseware whether in the form of a written manual,document, software program, service or otherwise belongs to the University of Newcastle upon Tyne ("the Owner") and may not be used, sold, licensed, transferred, copied or reproduced in whole or in part in any manner or form or in or on any media to any person other than in accordance with the terms of the Owner's Licence Agreement or otherwise without the prior written consent of the Owner. All use of this material is governed by the Owner's Standard Licence Agreement together with the appropriate Schedule. The following are available: A Standard Licence Schedule to cover all use including all for-profit use by any type of organisation and all use by non-educational establishments An Educational Licence Schedule for not-for-profit internal use only by a recognised educational establishment The Netskills logo and this copyright notice must be included in any copy or adaptation. Netskills is a trademark of Netskills, University of Newcastle
  2. This talk aims to give an overview of JavaScript - introducing the concepts and explaining the potential and possible uses. The talk moves on to describe the basics behind the language, how and where it operates and how it is included in a web page. A number of real examples of JavaScript are provided to demonstrate some of the features and syntax of the language, and to identify key concepts. As part of this, a quick refresher on the HTML form tags is given as this is crucial to many of the exercises in the accompanying hands-on document. A comparison with Java is given to clear up any confusion surrounding the connection between the two languages.
  3. When the web was conceived, browsers were limited to text and image - later tables and frames. The extent to which they provided interactivity with the user was very limited. JavaScript was developed by Netscape as a simple programming language (often referred to as a scripting language). It is easy to learn and small sections of JavaScript can be added to a web page rather than needing to develop complicated programs. It was specially designed for web page interaction and manipulating the web browser and page elements. It is often used to respond to user actions such as mouse clicks. Although developed by Netscape, and other variants exist, such as Jscript from Microsoft, a standard has been developed by the European Computer Manufacturers Association. It is known as ECMAScript, using the standard ECMA262, which can be found fully documented at the address on the slide.
  4. Examples should be shown which demonstrate different uses of JavaScript. For example, try the calculator example at the address shown. JavaScript can provide interactivity and/or enhance pages in several ways. It is particularly good at manipulating browser elements and images to improve page presentation and navigation. As a programming language it can handle quite complex calculations and control the behaviour of embedded content including images. A useful function is input validation - checking form responses BEFORE the form is sent for processing to a server. In this way it can be quicker and more user friendly, providing immediate feedback to the user. JavaScript can be combined with other technologies such as Java applets or plug-ins, but this is beyond the scope of this presentation.
  5. JavaScript is embedded/included within HTML. You can often see JavaScript in the source of a web page or it is provided for information on the page as with the calculator example. JavaScript is mainly used as a client-side language - it downloads with the web page. Once the page has downloaded and is on the users' machine, it is actually the web browser which then interprets the JavaScript instructions. JavaScript pages run quickly, you are not relying on an internet connection to a web server. Short pieces of JavaScript can be combined with HTML without the need to develop a fully blown program. There are two types of computer language, compiled and interpreted. To write or edit a compiled language requires a special piece of software called a compiler. JavaScript belongs to the other category, called interpreted. In the case of JavaScript, this interpretation is done by the browser software at run-time. Because JavaScript is interpreted, this means that no special tools are required to write or edit JavaScript, just a normal text editor. JavaScript web pages can be platform independent i.e. they will run on different browsers and computers (as long as the browser is JavaScript enabled). If you see a JavaScript web page that you like, you may be able to take that JavaScript and use it for your own purposes. (Remember to acknowledge the original author!)
  6. Java is often confused with JavaScript. JavaScript was originally called LiveScript, but due to the popularity of Java at the time, it was renamed JavaScript . Java is different from JavaScript in a number of significant areas: Java is a full programming language which can do just about anything - JavaScript isn't and can only do relatively simple things. Java is much harder to learn and takes much longer to master. Java is compiled. The language is not 'embedded' or written in the page - it is self-contained as a separate file. Java can be used/written totally independently from the web - JavaScript works with web browsers only. They are sometimes used together - JavaScript can be used to 'control' or configure Java 'applets'. This talk does not cover Java in any more detail. A separate Netskills Training Module investigates the use of Java applets.
  7. To learn JavaScript you will need to learn some of its language syntax. However, a good strategy is to learn the basics, and then use and adapt other people's JavaScript. There are plenty of sites on the internet offering free JavaScript (see slide 4) with the calculator example. Other useful addresses are provided in the notes of last slide. As with HTML, JavaScript can be written in a text editor and viewed in a browser. As it is a programming language the syntax is quite strict (compared to HTML). It is also a good idea to make sure your HTML is up to scratch as this will save you time. The hands-on exercises contain an refresher exercise on HTML forms.
  8. JavaScript can be contained either in the header section of an HTML page or in the body. This JavaScript statement is shown as a pure JavaScript statement within SCRIPT tags. Notice that there is no HTML in the body of this page at all. (Demonstrate what this JavaScript looks like in a web browser). This statement writes a line of text on a web page. The command document.write is a standard function in JavaScript to write text to the page. The following is a more technical explanation for background information only: document.write is derived from the JavaScript object model (not covered in detail here). It works on the principle that all document and browser elements have an object name (document, window, image etc) and can each has various properties that can be manipulated. The object hierarchy means that individual elements can be uniquely identified i.e. document.myform.mytext would refer to the text entry named mytext within the form called myform within the current page (document). The arrow symbol '' is used in these slides and in the workbook to indicate where a JavaScript statement should be typed on one line without a break. A line break in the wrong place will stop JavaScript from working.e.g. document.write('This is my first  JavaScript Page'); should actually be typed: document.write('This is my first JavaScript Page');
  9. This example demonstrates that anything included within the quotes in the document.write statement is printed to the screen, and this includes HTML tags. The <h1> tag is delivered to the browser along with the text, and the browser would interpret it as a normal HTML file, displaying the text in the Heading 1 style. IMPORTANT NOTE: This example shows a JavaScript statement in the <body> of the web page. It is possible to include JavaScript statements in the <head> section of a web page but care must be taken that they do not try to access items that don't exist until the page has loaded (e.g. form elements, links, images). The web browser parses (reads through and executes) any script commands as it displays the page. In most cases it is common sense that dictates where a statement should be placed. If, in the above example, document.write was placed in the <head> of the page, the text "This is my first JavaScript Page" would appear in the <head> of the finished page – this would be incorrect – although modern browsers will let you get away with it! In some circumstances you may wish to use document.write in the <head> - for example to dynamically generate <meta> or <title> tags. Such uses are not considered here. JavaScript functions are typically defined in the <head> section of a web page as they do not normally execute until they have been triggered elsewhere. The use of functions in JavaScript is covered in the Netskills Training Module: "Further JavaScript (Enhancing JavaScript with Functions and Events)"
  10. Compare this example with the previous one. This time the JavaScript is written inside the HTML tags and there are no <script> tags. In this case if the browser is JavaScript-enabled it will process the commands when it needs to. If the browser doesn't understand JavaScript it will ignore the extra code (it should see it as an HTML attribute that it cannot process and therefore ignores, although very old browsers my throw an error) This example demonstrates an HTML hyperlink, but notice the JavaScript enclosed within the <a href.. tag of the second link. onMouseOver is referring to an event. That is, this JavaScript will happen in response to something that the user does e.g click a button, or in this case, when they move the mouse over the link (this will not happen if you move your mouse over the first link!). window.alert will display what is called an alert box on the screen containing the text specified, in this case, "hello". The first link will behave normally. (See separate Netskills Training Module for more details on Functions and Events in JavaScript.)
  11. This example shows two separate statements. The first is some pure JavaScript so must be contained within <script> tags. It displays a pop up box with the message "Enter your name" and a space to type in text. This occurs immediately when the page loads. The second statement is within an HTML form. The <input> tag includes some JavaScript within it. The tag creates a form element (a button) and associates with it some JavaScript. The JavaScript responds to the mouse click event (onClick) on the button by producing a pop up alert box with the text 'Hello' in. This only occurs when the user triggers the event by clicking on the button. JavaScript makes use of both single and double quotes. They are used in this example to have a text string 'Hello' contained within another string which defines the code to be carried out in response to the onClick event. It is important that the quotes should match - if they don't, your JavaScript will not work!
  12. JavaScript is very useful for processing and manipulating user input and form elements. A common way of obtaining input is via the HTML <form> elements which can provide text entry boxes, selection boxes, menus and buttons. Form elements can be named and hence uniquely identified within the JavaScript object model.
  13. This example shows a simple form. Notice the name attribute is used at all points - to name the form, and to name each element within the form. How JavaScript uses the name attribute is described next.
  14. To refer to the value that a user has typed in a text box, you use the following naming system: document.formname.elementname.value This is a naming convention derived from the JavaScript object model: document refers to the page displayed in the browser. formname is supplied by the page author as the name attribute of the <form> tag - in the example it is addressform and refers to the whole form. elementname is supplied by the page author using the name attribute of the <input> tag. value is a predefined term which refers to the text typed in by the user.
  15. This simple code creates a form called alertform. The JavaScript is activated when 'Go' button is pressed (an onClick event - see separate Netskills Training Module for more details on Functions and Events in JavaScript). The current value of the element yourname would be displayed in a an alert box.
  16. Some tips for debugging and clarity to make it easier for you (the trainer) as well as for the audience. Follow the conventions we have used for capitalisation which will make it easier to read / find mistakes. References:An alternative site with ready made scripts for download: http://builder.com.com/ Follow links to Web Development and then JavaScript to see a number of good examples.