269200 Web Programming Language Week 4 Dr. Ken Cosh Introducing Javascript.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lesson 12- Unit L Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
CIS101 Introduction to Computing Week 09 Spring 2004.
JavaScript Teppo Räisänen LIIKE/OAMK HTML, CSS, JavaScript HTML defines the structure CSS defines the layout JavaScript is used for scripting It.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
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.
Dynamic Web Authoring Week3 – Javascript Basic COM311H Zheng, School of C&M, UUJ1.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Intro to JavaScript. Some simple examples Examples from our webpage Examples from Andrews webpage Today’s Example.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
1 Server versus Client-Side Programming Server-SideClient-Side.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript Introduction inf385t Semantic Web 2/20/2006.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
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.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
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.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Java Script Introduction. Java Script Introduction.
>> Introduction to JavaScript
Unit M Programming Web Pages with
Tutorial 10 Programming with JavaScript
JavaScript is a programming language designed for Web pages.
Unit M Programming Web Pages with
Donna J. Kain, Clarkson University
Exploring JavaScript Ch 14
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Introduction to JavaScript
JavaScript an introduction.
Web Systems Development (CSC-215)
A second look at JavaScript
Your 1st Programming Assignment
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Web Programming Language
Tutorial 10 Programming with JavaScript
Introduction to JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript Basics What is JavaScript?
Web Programming and Design
Presentation transcript:

Web Programming Language Week 4 Dr. Ken Cosh Introducing Javascript

Recap We’ve been looking at “Front End” development Using HTML & CSS to make the page look the way we want CSS for style CSS for layout

Javascript Enables Dynamic Functionality Pop Up when your mouse goes over it Updates Page Move objects around page Browser side Scripting language Runs in the browser Has access to all elements in the document

document.write(“Hello World”) Alternative is VBScript C++ Style Dot Notation No Semicolon

Where to Javascript? In the Body In the Head In a.js file

Debugging Use the Console! a = 5; b = 6; c = a + b; console.log(c); Be aware, that not all browsers will give the same console error message!

Comments // This is a comment! /* This is a longer set of comments Seem familiar? */

Semi Colons Semi colons are not necessary, unless you want to put more than one statement on a line; X +=10; y-=5; z=0 A new line terminates any statement But you can use semicolons if you like

Variables Weakly Typed Language Name can include a-z, A-Z, 0-9, $ and _ First character is not a number Case Sensitive

Strings Surrounded by “ or ‘ If necessary escape another quote using \ Message = ‘Hi I\’m Ken!’ A string can be assigned another strings value; stringa = stringb

Operators Arithmetic Operators +, -, *, /, %, ++, -- Assignment Operators =, +=, -=, *=, /=, %= Comparison Operators ==, !=, >, =, <=, === (equal to and of the same type), !== Logical Operators &&, ||, !

String Concatenation Uses the + symbol Name = “Ken” + “Cosh”

Types JavaScript is Loosely Typed The type is determined when a value is assigned to it, and the variable’s type can change

Functions function sum(a, b) { return a + b }

The DOM Document Object Model Different parts of the HTML are discrete objects And each object has its own properties and methods The dot notation is used to refer to properties or methods of an object

The DOM

A URL is the href part of an anchor tag somewhere in the document; Link Test Click Here url = document.links.mylink.href document.write(‘The URL is ‘ + url) (Note, this probably won’t work in IE!)

links links is an array of the links with in document, so we could reference it as; url=document.links[0].href We could find the size of the links array using numlinks = document.links.length So, we could do; for(i=0; i<document.links.length; i++) document.write(document.links[i].href + ‘ ’)

length length is a property of all arrays Including the history object, which contains a list of urls that the browser has visited. document.write(history.length) The history object has some functions, for example; history.go(-3) //Go back 3 pages history.back() history.forward()

getElementById A useful function is ‘getElementById’, which takes as its parameter the id given to a tag; url = document.getElementById(‘mylink’).href Because ‘getElementById’ is SO important, often it is replaced by the function ‘$’, so we could use; url = $(‘mylink’).href

$ function $(id) { return document.getElementById(id) }

If Statement if(a > 100) { document.write(“a is greater than 100”) } else if(a<100) { document.write(“a is less than 100”) } else { document.write(“a is equal to 100”) }

Switch Statement switch(page) { case “Home”: document.write(“You selected Home”) break case “About”: document.write(“You selected About”) break case “Links”: document.write(“You selected Links”) break }

The Ternary Operator (?) ? can used instead of if statements document.write(a <= 5 ? “a is less than or equal to 5” : “a is greater than 5”)

While Loops while (counter <5) { document.write(“Counter: “ + counter + “ ”) ++counter } What would happen without ++counter ?

Do … While count = 1 do { document.write(count + “ times 5 is “ + count * 5 + “ ”) } while (++count <= 5)

For Loops for(count = 1; count <=5; count++) { document.write(count + “ time 5 is “ + count * 5 + “ ”); }

With string = “The quick brown fox jumps over the lazy dog” with(string) { document.write(“The string is “ + length + “ characters ”) document.write(“In uppercase it is: “ + toUpperCase()) }

Arrays Should be pretty familiar to you; students = [‘John’, ‘Peter’, ‘Mike’]

Multi-dimensional arrays tictactoe = [[‘X’, ‘O’, ‘X’], [‘O’, ‘X’, ‘O’], [‘X’, ‘O’, ‘X’]] document.write(tictactoe[1][2])

Associative Arrays countries = {“uk”: “United Kingdom”, “th”: “Thailand”, “us”: “United States”} for (country in countries) document.write(country + “ = “ + countries[country] + “ ”)

Array Methods concat – concatenates 2 arrays fruit = [“Banana”, “Grape”] veg = [“Carrot”, “Cabbage”] document.write(fruit.concat(veg))

Array Methods push() pop() reverse() sort()

Defining Functions function function_name(parameters) { statements }

The arguments Array The arguments Array is a member of every function You can use it to find the number of arguments (parameters), and what they are.

Example displayItems(“Dog”, “Cat”, “Pony”, “Hamster”, “Tortoise”) function displayItems(v1, v2, v3, v4, v5) { document.write(v1 + “ ”) document.write(v2 + “ ”) document.write(v3 + “ ”) document.write(v4 + “ ”) document.write(v5 + “ ”) } What if we have more than 5 (or less than 5 items)?

Using arguments function displayItems() { for(i=0; i<displayItems.arguments.length; ++i) document.write(displayItems.arguments[i] + “ ”) }

Returning Values document.write(fixNames(“Kenneth”, “JOHN”, “cOSh”)) function fixNames() { var s = “” for (i=0; i<fixNames.arguments.length; ++i) s += fixNames.arguments[i].charAt(0).toUpperCase() + fixNames.arguments[i].substr(1).toLowerCase() + “ “ return s.substr(0, s.length-1) }

Returning Arrays words = fixNames(“kenneth”, “JOHN”, “cOSh”) for(i=0; i<words.length; ++i) document.write(words[i] + “ ”) function fixNames() { var s= new Array() for (i=0; i<fixNames.arguments.length; ++i) s[i] = fixNames.arguments[i].charAt(0).toUpperCase() + fixNames.arguments[i].substr(1).toLowerCase() return s }

Exercise – Form Validation Create a Registration Form The form should ask users to input the information to the right You will then need to validate the information that they input.

Exercise – Form Validation Forename – Must not be blank, must not contain spaces, and must have at least 3 alphabet characters Surname – same as for Forename Username – At least 5 characters and can include numbers, _ and – Password – Must be at least 8 characters, containing both upper and lower case letters, numbers and symbols. Age – It is a 18+ website, so the age must be between 18 and 110 – must be of the form

Impress Me! How about re-entering password to make sure it is the same? How about making it look good with css? How about indicating the required fields with a * - and have the star disappear when the entry is ok? How about date of birth with a calendar, rather than age? How about storing your js functions in a separate.js file?

But First… HTML Forms Forms can be used to pass data from one webpage to another Forms can contain input boxes, drop down menus, check boxes, radio buttons, etc.

HTML Forms Forename: To impress me, you might want to look at other input types!