1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

The Web Warrior Guide to Web Design Technologies
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
Information Technology Center Hany Abdelwahab Computer Specialist.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Computer Science 103 Chapter 4 Advanced JavaScript.
CIS101 Introduction to Computing Week 12 Spring 2004.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Javascript and the Web Whys and Hows of Javascript.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
JavaScript An Introduction.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
JavaScript Part 1.
JavaScript 1. What is JavaScript? JavaScript allows web authors to create dynamic pages that react to user interaction. It is an Object-based because.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Using Client-Side Scripts to Enhance Web Applications 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.
Intro to JavaScript. Some simple examples Examples from our webpage Examples from Andrews webpage Today’s Example.
1 JavaScript
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
JavaScript. JavaScript is a programming language used to make web pages interactive. JavaScript is scripting language used for client side scripting.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming control structures, events, objects.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
HTML Overview Part 5 – JavaScript 1. Scripts 2  Scripts are used to add dynamic content to a web page.  Scripts consist of a list of commands that execute.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
JAVASCRIPT A quick review. True False ■The DOM is a standardized way of referring to parts of a Web page. ■TRUE ■In the DOM, attributes have their own.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Third lecture Event 27/2/2016 JavaScript Tutorial.
CHAPTER 10 JAVA SCRIPT.
Web Development & Design Foundations with HTML5
Week#2 Day#1 Java Script Course
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript Intro.
Web Programming and Design
Presentation transcript:

1 Introduction to Javascript Peter Atkinson

2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt methods –window.write method –string, number and Boolean variables –functions –array objects –date objects –conditionals –multiple conditionals –while loop –for loop –objects, methods and events

3 Page Template Untitled Document

4 Using Alert Greeting Page Click here for greeting Embedded Javascript

5 What is a Variable? A variable is a container Create a variable var myname Creates a container called ‘myname’ Put something in to the container by assigning a value to the variable using = myname = “Fred”

6 Create a Variable and Assign a Value So you can create a variable and give it a value like this: var myname myname = “Fred” But, Javascript lets you make a shortcut by creating a variable and assigning it a value at the same time: myname = “Fred”

7 Points to Notice Javascript is case sensitive so: myName is a different variable to myname At the end of every line of Javascript code you do not need to put a ; but put one anyway. From now on: myname = “Fred”; is correct You can use double quotes “Fred” or single quotes ‘Fred’ as long as you use them consistently Javascript is “weakly typed” so you do not need to specify what kind of data will be stored in your variable

8 Let’s Use a Variable Using a Variable var myname; myname = “Fred”; Click for message telling you the name Hands On Rewite this page using document. write to write the name to the page when it loads

9 Types of Variable String –When you specify a text variable it must be enclosed in quote marks eg. “Fred” Number –Integer (no decimals) –Floating Point – has decimals Boolean – true or false –eg. myanswer = (2 + 2 ==3) –myanswer contains the value false Object

10 Manipulating Strings To join two strings, use the concatenation operator +: var mystring; mystring = “Flossie”; document.write(“My name is: “ + mystring); Hands On Put this code into an HTML page Modify it so that it obtains the user’s name using a prompt

11 Manipulating Number Variables Put this Javascript into a blank page: var mystring, mynumber, shownumber; mystring = "56"; mynumber = 44; shownumber = mystring + mynumber; document.write(shownumber); Hands On Alter this code using the parseInt() function so that it adds 56 and 44 and writes the result 100 to the page

12 Using Number Operators Be careful how you use numeric operators Examine this piece of code: var myanswer; myanswer = * 3; document.write(myanswer); Hands On What number do you think will be written to the page Test it. Were you right? If not, why not?

13 Arrays An array is like a variable but it can hold more than one value at a time var beatleArray = new Array(); beatleArray[0] = “John”; beatleArray[1] = “Paul”; beatleArray[2] = “George”; beatleArray[3] = “Ringo”;

14 Arrays Hands On Write a piece of code using an array containing the names of your four best friends Have your code write each of these pieces of data to your HTML page

15 Arrays Hands On – suggested solution myFriends = new Array(); myFriends[0] = "John"; myFriends[1] = "Paul"; myFriends[2] = "George"; myFriends[3] = "Ringo"; document.write(myFriends[0] + " "); document.write(myFriends[1] + " "); document.write(myFriends[2] + " "); document.write(myFriends[3]);

16 What is a Function? A function is a block of code that may be used over and over. The structure of a function is: function functionName() { code that does something; } So for example, a function that shows our greeting: function greeting() { alert(“Hello”); }

17 Using a Function Hands On: try this code Greeting Function function greeting() { alert("Hello"); } Click here for greeting What are the advantages of using a function?

18 Conditional if (test condition) { some code that executes if condition is true; } else { some other code that executes if condition is false; }

19 Example Conditional if (myage < 30) { document.write(“You are too young!”); } else { document.write(“You are too old!”); } Hands On Place this code in an HTML page with a prompt to collect the reader’s age

20 Operators Used in Conditionals Comparison Operators == Tests if LHS is equal to RHS < Tests if LHS is less than RHS > Tests if LHS is greater than RHS <= Tests if LHS is less than or equal to RHS >= Tests if LHS is greater than or equal to RHS != Tests if LHS is not equal to RHS Logical Operators && AND both LHS and RHS must be true || OR LHS or RHS must be true ! NOT reverses condition

21 Using a Conditional Hands On Use a prompt to obtain a number from the user Write some code to test the number using a comparison operator to return one of two messages Try each of the comparison operators in turn Put another prompt into your code to obtain a second number from the user Write some code that tests both numbers at the same time using a logical operator

22 Multiple Conditional Hands On : try out this code switch (myBeatle) { case “John”: alert(“Just Imagine!”); break; case “Paul”: alert(“Sorry about the divorce!”); break; case “George”: alert(“You are sadly missed!”); break; case “Ringo”: alert(“Where are you now?”); break; default: alert(“You are not a Beatle!”) break; }

23 For Loop for (initialise; test; update) { do something; } For example, we could write out the contents of an array: myArray = new Array(“John”,”Paul”’”George”,”Ringo”); for (i = 0; i < 4; i++) { document.write(myArray[i] + “ ”); } Hands On Try out this piece of code

24 For Loop Hands On Use prompts to collect data from the user and store it in an array Write the contents of the array to an HTML page

25 While Loop The while loop allows you to test a condition and keep looping while it is true while (test condition) { do something; }

26 While Loop Example Hands On: try this code var gameon = true; var i = 0; while(gameon == true) { var myguess = prompt("Guess what number between 1 and 5 I am thinking of: ",""); if (myguess == "3") { gameon = false; alert("You got it right! It is 3."); i++; } else { alert("You got it wrong! Try again."); i++; } document.write("You took " + i + " guesses!");

27 Function – Hands On Using Javascript features that you have learned so far, write some code for this game: User to pick a number from 1 to 5 If user guesses correctly, the user wins If user guesses incorrectly, user can guess again User can keep on guessing until user gets it right Page displays number of guesses the user had

28 Javascript Concepts Object – a car Attributes – colour, insurance group Methods – drive, reverse Events – start, stop, collision

29 Native Objects There are a number of built-in objects in Javascript eg Date and Array To create an Array eg. var myarray = new Array(); To create a Date object: var thisdate = new Date(); –Methods of the Date object: getDate() getDay() getMonth() getFullYear() eg. mydate = thisdate.getDate() Hands On Use the Date object and its methods to write today’s date to your HTML page in the format Tuesday 1 January 2007

30 Events Applies to the window object –onload – when window opens –onunload – on moving to another page –eg. window.onload = myFunction; Applies to all HTML elements –onmousedown – user depresses mouse button –onmouseover – user moves mouse onto element –onmouseout – user moves mouse off element –onclick – user clicks mouse –eg. Some text

31 Using Events Hands On Write a function that triggers an alert Call the function from each of the events listed in turn

32 Finding Bugs Hands On You have been provided with a file HandsOn1.html that contains some common coding errors Find the errors and correct them

33 Rollovers Hands On Copy the code from the Rollovers sheet into an HTML page Write notes on the sheet explaining the use of the if…else feature used in this code

34 Cycling Banner Hands On Copy the code from the Cycling Banner sheet into an HTML page Write notes on the sheet explaining the use of the setTimeout() feature used in this code

35 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt methods –window.write() method –string, number and Boolean variables –functions –array objects –date objects –conditionals –multiple conditionals –while loop –for loop –objects, methods and events