JavaScript An Introduction.

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

Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Java Script Session1 INTRODUCTION.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
The Web Warrior Guide to Web Design Technologies
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Introduction to scripting
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, Fifth Edition Chapter 1 Introduction to JavaScript.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
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.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
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.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Server versus Client-Side Programming Server-SideClient-Side.
1 JavaScript in Context. Server-Side Programming.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
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.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 6 JavaScript: Introduction to Scripting
Tutorial 10 Programming with JavaScript
Web Development & Design Foundations with HTML5
JavaScript is a programming language designed for Web pages.
Web Development & Design Foundations with HTML5 7th Edition
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
JavaScript What is JavaScript? What can JavaScript do?
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
CIS 136 Building Mobile Apps
Web Programming and Design
Presentation transcript:

JavaScript An Introduction

What is JavaScript? JavaScript adds “dynamic” behavior to HTML pages, adding programming capabilities. JavaScript is a predominantly "client side" scripting language used for a large variety of tasks, such as form validation, image rollovers and fly-out menus. JavaScript was created by Netscape, and is now the “official” client side language.

Where Does JavaScript Go? JavaScript code belongs inside HTML <script> tags. Inside the <script> tags, the rules of JavaScript apply. No HTML can go directly inside the script tag. JavaScript is embedded directly into the HTML page, usually in the <head> tag.

What Does JavaScript Look Like? The format of a typical JavaScript looks like this: <script type=“text/javascript”> //Code goes here </script> Notice the comment tag (//Code goes here) inserted in the text. The double slashes indicate no code will be executed beyond that point on the line.

Client Side vs. Server Side Server side scripting is used for interaction with the user, and is frequently used to create dynamic pages by interacting with a database. Client side scripting is designed to run on the user's machine, and as such is cut off from the server while it is running. Any work that can be done on the client is best done there, as any processing there takes a load off a busy server.

JavaScript Statements JavaScript expects us to speak to it in lines of code it understands, called “statements” Each JavaScript statement should go on the same “line” of text, and most end with a semi-colon: var myAnswer = 2 + 2;

Browser Differences There are differences in how browsers are designed, and therefore differences in how JavaScript is processed. One such difference appears in how the document object model (DOM) is handled. The DOM allows us to navigate an HTML document, and make changes to the page, without reloading the page.

No Errors Shown, By Design Because JavaScript errors are so common and disruptive to a user’s experience, most browsers elect NOT to display any JavaScript errors, by default In order to troubleshoot scripting errors, we use a browser like Firefox, which gives feedback on errors by selecting: Tools >> Error Console

Overly Sensitive JavaScript is case sensitive, some say “overly” so. If you use a JavaScript function or variable and get even one letter in the wrong case, JavaScript will either show you an error, or will give you unexpected results

Data Types Numerical data is entered in JavaScript without quotations around it, while alphanumeric data (hereafter called string data) needs to be inserted with quotes. There are 3 basic types of data in JavaScript, strings, numbers and booleans. While the JavaScript engine is processing, it will give it's best guess as to the data type involved. In general, any number that can be used in a calculation is considered numerical. An address number, like a zip code, would be a string.

Numerical Data While strings must be quoted, numbers must not be, if they are to be calculated. JavaScript treats most numbers automatically, meaning there is one numerical data type

Strings Of Data In JavaScript, user data, like names or HTML look like “strings” of data that is NOT specifically meaningful to JavaScript. This data type is called a “string” because it is alphanumeric data that is “strung” together like beads on a chain Strings are required to be in quotes, either single or double quotes, but not both

Variables Variables are programming constructs that have contents inside them that ‘vary’ Variables allow us to tailor our code to the circumstances: var myAnswer = 2 + 2; In the above code, a variable named ‘myAnswer’ is declared, and it is assigned a value, the sum of 2 and 2.

Concatenation Concatenation is a programmer’s word for ‘connect’. When we use variables, we ‘concatenate’ data into meaningful statements: myVar = ‘The Year is: ‘; myVar += myDate.getFullYear();

Operators JavaScript uses “operators” which allow us to make mathematical calculations, and also to concatenate strings of data: var myVar = 2 + 2; In the above example the “plus” sign is the operator that allows us to add 2 and 2. The same operator is used to concatenate strings: var myName = “Bill ” + “Newman”; Note the “space” left after the first name. This is there so that the two words have a space between them, once they are concatenated.

Keywords Keywords are words that have special meaning in JavaScript. These words must be used in accordance to JavaScript rules and can't be used as variable or function names. Some common keywords are break, do, else, for, this, true, false, if, var, void, null, return, while, function, in and switch. If we are tempted to use a word that logically is very close to a keyword, the suggestion is to attach the prefix "my" to it, ie: myFunction.

Read Right To Left Isn’t it odd that the statements read right to left? var myVar = 2 + 2; This is because the most important part of the statement (the sum, if it’s math) is easier to scan this way in a long program.

JavaScript Functions JavaScript has several built in “functions” which allow us to perform actions automatically by calling the name of the function, and supplying some data. Here is a very common function, called “alert()”. alert(‘Here is a message that pops up!’); Note the function includes a “string” of data, that is a message to a user

Anatomy Of A Function For a function to “function” certain rules must be followed: Functions all require parenthesis: alert(“message here”); Sometimes they require “data” inserted inside the parenthesis. In this case, the “string” of text saying, “message here”.

Booleans Boolean data gets its name from the Mathematician George Boole Boolean data consists of logic types. For us, that would be the values true and false Boolean data, like numerical data, does NOT use quotation marks Booleans (true or false) are always lower case If(x == true){alert(“It’s true!!”);}

Equal? NOT! An important operator, often associated with Booleans, is the NOT operator, symbolized by the exclamation mark “!” The “not” operator is used with the equal sign to create “not equal”, which is the same as false: If(x != true){alert(“That would be false!”);}

Late Binding JavaScript reads and processes statements one at a time. JavaScript makes assumptions about the code it sees, since it processes data on the fly. If it sees numbers that are not quoted, it tries to calculate them. If it sees numbers that are quoted, it tries to connect, or concatenate them

Curly Braces Curly braces (the “{“ and the “}” signs) are required to let JavaScript know where to start and stop. These are required when you build your own functions, and for if statements, for example: if(x == y){alert(“x equals y!”);}

Conditionals JavaScript allows us to do different things according to different circumstances We do this as people, when we say: If it rains, bring an umbrella. Otherwise, bring shorts. A ‘conditional’ is a statement that allows us to determine what happens “if” something occurs

If Statement Here is a sample ‘If’ statement: If(x == y) { alert(“I guess x equals y!”); } The “double equal” is the operator that checks for equality. If the variables x and y are equivalent, then the alert displays

Assignment vs. Comparison The “equals” sign carries special significance in JavaScript To “compare” data, we need to use the “double” equal: if(x == y){alert(“x equals y!”);} To “assign” data, we use a single equal sign: var myVar = 2 + 2; var myName = “Bill “ + “Newman”; Both of these examples “assign” data to the variable on the left.

Custom Message Based On Date The sample below accesses the date object on the user’s machine, and displays an “alert()” message, if it’s noon, or later: var myDate = new Date(); If(myDate.getHour() > 12) { alert(‘It’s after Noon!’); }

JavaScript Objects JavaScript uses “objects” to allow us to access resources like the “date” on the system clock of the user’s machine: var myDate = new Date(); We need to create an ‘instance’ (example) of a date object in order to use it further. This allows us to work with different date objects at once.

The Date Object Users come to our website from different parts of the world Accessing the user’s system clock is useful for tailoring a user’s experience to their time of day What if you wanted to show a custom image or message based on the time of day of the user? JavaScript does this.

Document Object Model (DOM) Document Object Model: The elements that make up an HTML document can be put to various purposes by JavaScript.  The way that an HTML document is navigated is by a structure called the Document Object Model (DOM).  The DOM allows us to read down to a part of an HTML document and either read or change data.  You can call out the object by it's exact name, or by climbing the DOM "tree" elements sequentially.

The Navigator Object JavaScript can determine information about what browser is being used. This is useful for tailoring HTML for a specific browser: var myBrowser = navigator.appName(); alert(“Your browser is: “ + myBrowser);

Event Handlers Events are occurrences on an HTML page, like a “page load” or the actions by the user, like mouse clicks and mouse hovers, etc. Events can be “captured” and used as a trigger for actions taken by JavaScript. The mouse clicks and mouse hovers, etc. are called "events", and the JavaScript "handles" or reacts to the actions of the user, hence the phrase "event handler".

Rollovers The most common kind of event handler is a “rollover", where an image is "swapped out" (changed) when a user hovers the mouse over an image. The simple "mouse over" example code below simply (although inefficiently) handles a mouse over event: <a href="mypage.htm" onMouseOver="document.myImage.src='images/on.gif'" onMouseOut="document.myImage.src='images/off.gif'"><img src="images/on.gif" name="myImage"><a/> The above example uses the “document” object model to identify an object by name, in this case an image named “myImage”