HTML Forms JavaScript Introduction / Overview Lab Homework #1 CS 183 4/8/2010.

Slides:



Advertisements
Similar presentations
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
The Web Warrior Guide to Web Design Technologies
Tutorial 10 Programming with JavaScript
Web Page Behavior IS 373—Web Standards Todd Will.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1 تدریس طراحی وب برای اطلاعات بیشتر تماس بگیرید تاو شماره تماس: پست الکترونیک :
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
Client-Side programming with JavaScript 3
Javascript and the Web Whys and Hows of Javascript.
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.
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.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Introduction to JavaScript Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan. 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
JavaScript - A Web Script Language Fred Durao
LOGO Introduction to Client-Side Scripting and JavaScript CHAPTER 9 Eastern Mediterranean University School of Computing and Technology Department of Information.
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.
By Tharith Sriv. To write a web page you use: HHTML (HyperText Markup Language), AASP (Active Server Page), PPHP (HyperText Preprocessor), JJavaScript,
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
1 JavaScript in Context. Server-Side Programming.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
In God we trust Learning Java Script. Java Script JavaScript is the scripting language of the Web! JavaScript is used in millions of Web pages to improve.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
JavaScript Lectures Level 7. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting.
Web Development JavaScript. Introduction to JavaScript.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Chapter 5: Intro to Scripting CIS 275—Web Application Development for Business I.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Introduction JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer,
Introduction to Client-Side Scripting and JavaScript
Web Development & Design Foundations with HTML5
My First Web Page document.write("" + Date() + ""); To insert.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
JavaScript an introduction.
My First Web Page document.write("" + Date() + ""); To insert.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
CS105 Introduction to Computer Concepts
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
Web Programming– UFCFB Lecture 13
CS105 Introduction to Computer Concepts JavaScript
Presentation transcript:

HTML Forms JavaScript Introduction / Overview Lab Homework #1 CS 183 4/8/2010

HTML Forms A form is an area that can contain form elements. Form elements are elements that allow the user to enter information (like text fields, text area fields, drop-down menus, radio buttons, checkboxes, etc.) in a form. A form is defined with the tag.. input elements.

Input The most used form tag is the tag. The type of input is specified with the type attribute. First name: Last name:

More form elements Radio buttons Male Female Check boxes I have a bike: I have a car: I have an airplane:

Action Attribute When the user clicks on the "Submit" button, the content of the form is sent to the server. The form's action attribute defines the name of the file to send the content to. The file defined in the action attribute usually does something with the received input. Username:

Form Tags

What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight programming language JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a license Not the same as Java! JavaScript's official name is ECMAScript. Try: ◦ ◦ (Slides are based on that tutorial)

History The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all Netscape and Microsoft browsers since Since 1997 standardized as ECMAScript (ECMA 262 or ISO/IEC 16262, ECMA 290, ECMA 327 and ECMA 357 or ISO/IEC DIS 22537) Gained lots of popularity, broad acceptance ECMAScript also in Flash The development of the standard is still in progress.

What does it do? gives HTML designers a programming tool can put dynamic text into an HTML page ◦ document.write(" " + name + " ") can react to events can read and write HTML elements can be used to validate data in forms can be used to detect the visitor's browser can be used to create cookies

Language Properties Object based (but not object oriented) Familiar scripting syntax Not strictly typed JavaScript is case sensitive A JavaScript statement is a command to a browser It is normal to add a semicolon at the end of each executable statement, but semicolon is optional, using EOL termination. JavaScript code is a sequence of JavaScript statements Each statement is executed by the browser in the sequence they are written. JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and ends with a right curly bracket }. The purpose of a block is to make the sequence of statements execute together, mostly used for grouping things together in a function or to specify a list of statements when a condition is met. JavaScript comments: // (single line), and /* …. */ (multi-line)

DOM (Document Object Model) Represents an object centric view of a document Platform independent API for HTML or XML elements

JavaScript Integration To insert a JavaScript into an HTML page, we use the tag Inside the tag we use the type attribute to define the scripting language Can appear anywhere in the doc, in HEAD, BODY, or in both...

Document.write document.write("Hello World!");

Support for older Browsers Browsers that do not support JavaScript, will display JavaScript as page content. Use HTML comment tag to "hide" the JavaScript.

Scripts in Scripts in Scripts to be executed when they are called, or when an event is triggered, are placed in functions Put your functions in the head section, this way they are all in one place, and they do not interfere with page content. function message() { alert("This alert box was called with the onload event"); }

Scripts in Scripts in If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section. document.write("This message is written by JavaScript");

Using an External JavaScript Sharing of JS over several pages possible Use include mechanism, put code in file: ◦ Save the external JavaScript file with a.js file extension ◦ The external script cannot contain the tags! ◦ To use the external script, point to the.js file in the "src" attribute of the tag:

JavaScript Variables JavaScript variables are used to hold values or expressions A variable can have a short name, like x, or a more descriptive name, like personName. Variable names are case sensitive (y and Y are two different variables) Variable names must begin with a letter or the underscore character You can declare JavaScript variables with the var statement: ◦ var x; var personName; After the declaration shown above, the variables are empty (they have no values yet), or ◦ var x=5; var personName=”Hans"; When you assign a text value to a variable, use quotes around the value If you don’t declare a variable they will be automatically declared: ◦ X = 5; If you redeclare a JavaScript variable, it will not lose its original value. ◦ var x=5; var x; You can do arithmetic operations with JavaScript variables: ◦ z = x + 5;

JavaScript Operators The assignment operator = is used to assign values to JavaScript variables The arithmetic operator + is used to add values together. y=5; z=2; x=y+z; The value of x, after the execution of the statements above is 7.

JavaScript Arithmetic Operators Assume, y=5

JavaScript Assignment Operators Assume x=10, y=5

The + Operator on Strings To add two or more string variables together, use the + operator. txt1="What a very"; txt2="nice day"; txt3=txt1+txt2; variable txt3 contains "What a verynice day” If you add a number and a string, the result will be a string! x=5+5; document.write(x); x="5"+"5"; document.write(x);

JavaScript Comparison Operators Assume x=5 if (age<18) document.write("Too young");

JavaScript Logical Operators Assume x=6, y=3

JavaScript Conditional Operator The conditional operator assigns a value to a variable based on some condition. variablename=(condition)?value1:value2 Example: ◦ greeting=(visitor=="PRES")?"Dear President ":"Dear ";

If … Else Statements Conditional statements are used to perform different actions based on different conditions. if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

Example (if … else) //If the time is less than 10, you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date(); var time = d.getHours(); if (time

Example (switch) //You will receive a different greeting based //on what day it is. Note that Sunday=0, //Monday=1, Tuesday=2, etc. var d=new Date(); theDay=d.getDay(); switch (theDay) { case 5: document.write("Finally Friday"); break; case 6: document.write("Super Saturday"); break; case 0: document.write("Sleepy Sunday"); break; default: document.write("I'm looking forward to this weekend!"); }

JavaScript Popup Box An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. function show_alert() { alert("I am an alert box!"); }

Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. function show_confirm() { var r=confirm("Press a button"); if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); } }

JavaScript Functions To keep the browser from executing a script when the page loads, you can put your script into a function. A function contains code that will be executed by an event or by a call to the function. You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external.js file). Functions can be defined both in the and in the section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the section. function functionname(var1,var2,...,varX) { some code }

Example function displaymessage() { alert("Hello World!"); }

The Return Statement The return statement is used to specify the value that is returned from the function. function product(a,b) { return a*b; } document.write(product(4,3));

JavaScript loops for - loops through a block of code a specified number of times, used when you know in advance how many times the script should run for (var=startvalue;var<=endvalue;var=var+increment) { code to be executed } while - loops through a block of code while a specified condition is true while (var<=endvalue) { code to be executed } Do.. While(), break, continue also exist and can be used accordingly

Will cover later: ◦ For.. In ◦ Events ◦ Try.. Catch ◦ Throw ◦ Special Characters ◦ Objects

Lab Homework #1 Add some JavaScript to your HTML page from lab homework #0 to validate form input: ◦ Add a HTML form to it for entering a person’s first name, last name, , age ◦ Form’s action attribute should be empty, since we don’t have PHP yet to do something useful ◦ Upon submission (user clicks submit button) a JS function should be called ◦ The function should verify:  Does the first name exist?  Does the last name exist?  Does the exist, and does it have  Does the age exist and is it a valid number? ◦ When there is an error in the form data, an alert box should be shown, prompting the user to fix the particular error ◦ Hint:  use document.getElementsByName to access form field values  Use return ‘false’ to prevent form submission ◦ If there is no error an alert box should be shown that everything is correct, and then submission should be done (we just simulate this for now) ◦ Submission as in previous lab homework, need URL of the HTML file ◦ Will use firebug to debug, script should have no JS errors ◦ Due: Next Tuesday in class (4/13)