DOM and timers CS380 1. Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.

Slides:



Advertisements
Similar presentations
HIGH LEVEL OVERVIEW: CSS CSS SYNTAX CONSISTS OF A SET OF RULES THAT HAVE 3 PARTS: A SELECTOR, A PROPERTY, AND A VALUE. IT’S NOT NECESSARY TO REMEMBER THIS.
Advertisements

The DOM tree CS The DOM tree CS380 2 Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in.
JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
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.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Cascading Style Sheets. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. CSS is a.
Chapter 8 Creating Style Sheets.
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
Tutorial 3 Introducing Cascading Style Sheets. XP New Perspectives on Blended HTML, XHTML, and CSS2 Objectives Learn about Cascading Style Sheets Write.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
© Ms. Masihi 1.  A web page is created using a language called, Hypertext Markup Language, better known as HTML Code.  HTML is a user friendly language.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Definition from Wikipedia.  The Prototype JavaScript Framework  implemented as a single file of JavaScript code  named prototype.js (
JQuery CS 268. What is jQuery? From their web site:
INTRODUCTION TO CLIENT-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes.
Using Cascading Style Sheets. Introduction to Styles and Properties  Cascading Style Sheets (CSS) are a standard set by the World Wide Web Consortium.
Headings, Paragraphs, Formatting, Links, Head, CSS, Images
JQuery Page Slider. Our goal is to get to the functionality of the Panic Coda web site.Panic Coda web site.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Unobtrusive JavaScript
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
CSE 154 LECTURE 19: EVENTS AND TIMERS. Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
The DOM tree CS The DOM tree CS380 2 Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in.
Lab 3: Language Structures User Interface Lab: GUI Lab Sep. 9 th, 2014.
Cascading Style Sheets Orientation Learning Web Design: Chapter 11.
Tutorial 5 Formatting with CSS. Objectives Session 5.1 – Evaluate why CSS styles are used – Determine where to write styles – Create an element selector.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CSE 154 LECTURE 18: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
Chapter 7: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
CSE 154 Lecture 20: The DoM tree.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
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.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
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.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
JavaScript Challenges Answers for challenges
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
Spiderman ©Marvel Comics Creating Web Pages (part 1)
Lecture 8: Events and timers
Events CS The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.
CSE 154 LECTURE 7: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Week 3: Introduction to jQuery and Adv. Javascript.
JQUERY AND AJAX
JavaScript IV Robin Burke ECT 270. Outline Final project reminder Style review Manipulating style Special effect examples.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
CSE 154 LECTURE 10: MORE EVENTS. Problems with reading/changing styles Click Me HTML window.onload = function() { document.getElementById("clickme").onclick.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Changing CSS Styles via JavaScript No readings?. 2 Review? You can change the CSS styling of an element with JavaScript Syntax is similar to accessing.
Chapter 13: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
CSS Introductions. Objectives To take control of the appearance of a Web site by creating style sheets. To use a style sheet to give all the pages of.
Introduction to.
Lecture 7: The Document Object Model (DOM); Unobtrusive JavaScript
Lecture 9: Events and timers
14 A Brief Look at JavaScript and jQuery.
© 2015, Mike Murach & Associates, Inc.
Lecture 7: Unobtrusive JavaScript
CSc 337 Lecture 9: The DoM tree.
Lecture 8: Events and timers
Lecture 7: Unobtrusive JavaScript
Web Programming and Design
Web Programming and Design
Modifying HTML attributes and CSS values
Presentation transcript:

DOM and timers CS380 1

Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code doesn't always work the same way in every browser code that works great in Firefox, Safari,... will fail in IE and vice versa  many developers work around these problems with hacks (checking if browser is IE, etc.) CS380 2

Prototype framework  the Prototype JavaScript library adds many useful features to JavaScript:  many useful extensions to the DOM  added methods to String, Array, Date, Number, Object  improves event-driven programming  many cross-browser compatibility fixes  makes Ajax programming easier (seen later) <script src=" ototype.js " type="text/javascript"> JS CS380 3

The $ function  returns the DOM object representing the element with the given id  short for document.getElementById("id")  often used to write more concise DOM code: CS380 4 $("id") JS $("footer").innerHTML = $("username").value.toUpperCase(); JS

DOM element objects CS380 5

DOM object properties 6 Hello, I am very happy to see you! HTML

DOM properties for form controls 7 Freshman? HTML

Abuse of innerHTML  innerHTML can inject arbitrary HTML content into the page  however, this is prone to bugs and errors and is considered poor style CS380 8 // bad style! var paragraph = document.getElementById("welcome"); paragraph.innerHTML = " text and link "; JS

Adjusting styles with the DOM  contains same properties as in CSS, but with camelCasedNames  examples: backgroundColor, borderLeftWidth, fontFamily CS380 9 window.onload = function() { document.getElementById("clickme").onclick = changeColor; }; function changeColor() { var clickMe = document.getElementById("clickme"); clickMe.style.color = "red"; } JS Color Me HTML

Common DOM styling errors  forgetting to write.style when setting styles: CS var clickMe = document.getElementById("clickme"); clickMe.color = "red"; clickMe.style.color = "red"; JS  style properties are capitalized likeThis, not like- this: clickMe.style.font-size = "14pt"; clickMe.style.fontSize = "14pt"; JS  style properties must be set as strings, often with units at the end: clickMe.style.width = 200; clickMe.style.width = "200px"; clickMe.style.padding = "0.5em"; JS

{Pimp my text}  Add JavaScript code (and any necessary modifications to the XHTML) so that when the user clicks the "Bigger Pimpin'!" button, the size of the text in the main text area changes to 24pt. CS380 11

{Pimp my text}  Hint: Remember that most CSS properties translate directly into properties of the.style property within that element's DOM object. For example, the following CSS color declaration: #id { color: red; }  …translates into: $("id").style.color = "red"; 12 CS380

{Pimp my text}  You are now going to add an event handler so that when the user checks the "Bling" checkbox, the main text area will have some styles applied to it.  Add JavaScript code (and any necessary modifications to the XHTML) so that when the user checks the box, the text in the text area becomes bold. You can test whether a given checkbox or radio button is checked by examining the checked property of the box's DOM object. When the box is unchecked, the style should go back to normal. 13 CS380

{Pimp my text}  Once you get the bold aspect to work, add the following additional effects to also take place when the Bling checkbox is checked:  underline the text (this is the CSS text-decoration property)  change its color to green  make it blink (this is also the CSS text-decoration property)  any two other styles of your choice 14 CS380

{Pimp my text}  Now we will transforming or "Snoopifying" the actual content of the text.  Write a new button named Snoopify that, when clicked, modifies the text in the text area by capitalizing it and adding an exclamation point to the end of it. You will want to use the value property of the text area's DOM element. 15 CS380

{Pimp my text}  Modify your Snoopify button so that it also adds a suffix of "-izzle" to the last word of each sentence. (A sentence being a string of text that ends in a period character, ".".)  Do this using the String/array methods split and join. For example, if you wanted to change all spaces with underscores in a string, you could write: var str = "How are you?" var parts = str.split(" "); // ["How", "are", "you?"] str = parts.join("_"); // "How_are_you?" 16

Unobtrusive styling CS function okayClick() { this.style.color = "red"; this.className = "highlighted"; } JS.highlighted { color: red; } CSS  well-written JavaScript code should contain as little CSS as possible  use JS to set CSS classes/IDs on elements  define the styles of those classes/IDs in your CSS file

Timer events CS  both setTimeout and setInterval return an ID representing the timer  this ID can be passed to clearTimeout/Interval later to stop the timer

setTimeout example CS function delayMsg() { setTimeout(booyah, 5000); $("output").innerHTML = "Wait for it..."; } function booyah() { // called when the timer goes off $("output").innerHTML = "BOOYAH!"; } JS Click me! HTML

setInterval example CS var timer = null; // stores ID of interval timer function delayMsg2() { if (timer == null) { timer = setInterval(rudy, 1000); } else { clearInterval(timer); timer = null; } function rudy() { // called each time the timer goes off $("output").innerHTML += " Rudy!"; } JS Click me! HTML

Passing parameters to timers CS function delayedMultiply() { // 6 and 7 are passed to multiply when timer goes off setTimeout(multiply, 2000, 6, 7); } function multiply(a, b) { alert(a * b); } JS  any parameters after the delay are eventually passed to the timer function  why not just write this? setTimeout(multiply(6 * 7), 2000); JS

Common timer errors CS setTimeout(booyah(), 2000); setTimeout(booyah, 2000); setTimeout(multiply(num1 * num2), 2000); setTimeout(multiply, 2000, num1, num2); JS  what does it actually do if you have the () ?  it calls the function immediately, rather than waiting the 2000ms!

Even More Gangsta  Set a timer so that when the Bigger Pimpin' button is clicked, instead of just increasing the font size once, it will repeatedly increase the font size by 2pt every 500ms. CS380 23

Even More Gangsta  (really tricky) Add a "Malkovitch" button that causes words of five characters or greater in length in your main text area to be replaced with the word "Malkovich". Consider compound words (i.e., contractions or hyphenated terms) to be separate words. (Inspired by the Malkovitch Mediator by Ka- Ping Yee, inspired by the film Being John Malkovitch)Malkovitch MediatorBeing John Malkovitch CS380 24

Even More Gangsta  Add an "Igpay Atinlay" button that converts the text to Pig Latin. The rules of Pig Latin are:  Words beginning in a consonant (or consonant cluster) have that consonant (or consonant cluster) moved to the end and -ay tacked on following.  Words beginning in a vowel simply have -ay tacked on the end.  Add any other crazy styling or pimpin' you want to the page. Show us your best stuff, playa! CS380 25