A little more JavaScript??. Building a caculator We'll need to learn about the following: – Form buttons. – Applying CSS rules by attribute values. –

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

CSS Layout Crash Course An Advance CSS Tutorial. Inline vs. Block Many HTML elements have a default display setting of Block. Block elements take up the.
Layout using CSS. Using multiple classes In the head:.important{font-style:italic;}.title{color:red;} In the body: Here's a type of paragraph that is.
Presenter: James Huang Date: Sept. 26,  Introduction  Basics  Lists  Links  Forms  CSS 2.
HTML – A Brief introduction Title of page This is my first homepage. This text is bold  The first tag in your HTML document is. This tag tells your browser.
Recognizing the Benefits of Using CSS 1. The Evolution of CSS CSS was developed to standardize display information CSS was slow to be supported by browsers.
Tutorial 4: Creating page layout with css
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Chapter 6 Basic forms 1. Forms and query string 2 form : a group of user input (UI) controls that accepts information from the user and sends the information.
Lecture Cascading Style Sheets (CSS) Internal Style Sheets Classes.
JavaScript Forms Form Validation Cookies CGI Programs.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
Computer Science 103 Chapter 4 Advanced JavaScript.
4.01 Cascading Style Sheets
CS 174: Web Programming February 26 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
Principles of Web Design 6 th Edition Chapter 4 – Cascading Style Sheets.
Key question  What are the three standardized core languages used to implement web pages? Write the full name not the abbreviation and describe the “layer”
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Principles of Web Design 6 th Edition Chapter 10 – Data Tables.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Web Development & Design Foundations with XHTML
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Chapter 12 Cascading Style Sheets: Part II The Web Warrior Guide to Web Design Technologies.
IDs versus Classes Naming Your Tags for Maximum Functionality.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
Ch 10 HTML and CSS Web Standards Solutions A Web Standardistas’ Approach.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
Advanced Web Development Instructor: Thomas Bombach.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
JavaScript, Fourth Edition
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.
Unit 10 – JavaScript Validation Instructor: Brent Presley.
Web Design (12) CSS Introduction. Cascading Style Sheets - Defined CSS is the W3C standard for defining the presentation of documents written in HTML.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
This is our seminar JavaScript And DOM This is our seminar JavaScript And DOM.
CSS Cascading Style Sheets *referenced from
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 7.  Change body and link styles  Create a drop-down menu  Change color and font styles in the menu  Create a pop-up effect using CSS  Utilize.
JavaScript and Ajax (JavaScript Dynamic HTML (DHTML)) Week 7 Web site:
>> CSS Layouts. Recall Properties of Box – Border (style, width, color) – Padding – Margin – Display – Background – Dimension Welcome Padding Area Border.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
Creating Web Documents CSS examples (do more later) Lab/Homework: Read chapters 15 & 16. Work on Project 3, do postings.
Build in Objects In JavaScript, almost "everything" is an object.
4.01 Cascading Style Sheets
Chapter 6 JavaScript: Introduction to Scripting
JavaScript functions.
Cascading Style Sheets (Layout)
JavaScript Basics Topics Overview Syntactic Characteristics
CSS Borders and Margins.
Event Driven Programming & User Defined Functions
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
HTML and CSS MIS 2402 Jeremy Shafer Department of MIS
JavaScript Basics Topics Overview Syntactic Characteristics
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript Basics Topics Review Important Methods Writing Functions
4.01 Cascading Style Sheets
CSc 337 Lecture 5: Grid layout.
Web Programming and Design
Web Programming and Design
Introduction to Web programming
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Positioning Boxes Using CSS
Presentation transcript:

A little more JavaScript??

Building a caculator We'll need to learn about the following: – Form buttons. – Applying CSS rules by attribute values. – The onkeypress attribute. – The String.fromCharcode() method. – The Number class. – Javascript keywords: switch, case, and break

body!

input { padding: 0; margin: 0; }.calculator { width: 400px; height: 500px; background-color: gray; border: 4px solid black; } #calculator_screen { width: 398px; height: 100px; font-size: 96px; font-family: Arial; text-align: right; } input[type="button"] { width: 100px; height: 99px; float: left; font-size: 80px; font-family: Arial; }

The action attribute specifies where to send the form-data when a form is submitted. In HTML5, the action attribute is no longer required… Basically, by using “#” – when form is submitted we keep the same thing in focus (the calculator!)…

Input Buttons You can create buttons with elements, by setting type="button". The text of the button is set with the value attribute. These buttons do not actually do anything – Their primary purpose is to trigger Javascript with the onclick attribute.

Attribute selectors You can create CSS rules which apply to tags with specific attribute/value pairs by using the square brackets ([]). A input[type="button"] selector chooses all the buttons on a page, which is exactly what we need for our calculator.

onkeypress attribute The onkeypress attribute – triggers whenever the user pushes a key. an event is generated with the specifics of the keypress. In most browsers, event.which contains which key was pressed. – IE uses which.keyCode instead. The event gives you the ASCII code. – some keys do not have associated characters. If the code executed by onkeypress returns false, the keystroke will be discarded. Otherwise it will be processed by the browser normally.

body!

body!

what do they look like? var total = 0; var last_operator = null; function add_digit(digit) { document.calculator.display.value += digit; } function queue_operator(operator) { do_operation(last_operator); document.calculator.display.value = ''; last_operator = operator; }

One thing to notice… One thing to note is that when we get an operator (like +), we can't process it until we get the next operator. Or an equals, or a C! It’s not a perfect calculator!

function clear_button() { document.calculator.display.value = ''; total = 0; last_operator = null; } function equals_button() { do_operation(last_operator); document.calculator.display.value = total; total = 0; last_operator = null; }

function do_operation(operator) { switch(operator) { case '+': total += new Number(document.calculator.display.value); break; case '-': total -= new Number(document.calculator.display.value); break; case '*': total *= new Number(document.calculator.display.value); break; case '/': total /= new Number(document.calculator.display.value); break; case null: total = new Number(document.calculator.display.value); }

function handle_key(e) { var k = String.fromCharCode(e.which); switch(k) { case '0': add_digit('0'); break; case '1': add_digit('1'); break; … case '9': add_digit('9'); break; … case '*': queue_operator('*'); break; case '/': queue_operator('/'); break; case '=': case '\r': equals_button(); break; default: return true; } return false; }

Getting the character! The helper methods in the String class are pretty fun! Specifically, fromCharCode takes the ASCII values and turns them into a string!

in order to apply an operation… We need numbers! Can you see where this is happening?? You can see more about them here… – _number.asp

Just when you thought you had it! Often, we want to perform assignment operations like the following: x = x + y; There is a shorthand for such operations: x += y; Similarly, we have -=, *=, and /= for subtraction, multiplication, and division. x += 1; can be shortened even further to x++;. Similarly for x -= 1; and x--;

switch The switch keyword is used when you want to branch on many possible values for a variable. For each value you want to check, you need a case. The break keyword halts execution of the current statement block, jumping to the first statement after the block. If none of the specific cases match the variable, then the switch jumps to default if it exists.

Switch, case, break