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
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.
Advertisements

JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
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.
CMPT241 Web Programming Intro to JavaScript. Project 4.
PhpXperts What is jQuery Javascript Library Fast and concise Simplifies the interaction between HTML and JavaScript.
Headings, Paragraphs, Formatting, Links, Head, CSS, Images
Learning & Development Telerik Software Academy.
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.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
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.
Interacting with a Web Page using JavaScript Mat Kelly GTAI Presentation January 10, 2014.
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.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
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.
JavaScript Frameworks Presented by Kyle Goins Also see:
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.
. Taught by: Muhammad Ali Baloch midahot. WHAT IS JQUERY JQuery is a fast, small, and feature-rich JavaScript library. Simplifies the interaction between.
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
DOM Animation Changing the DOM objects over time.
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.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
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.
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.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
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.
Welcome to jQuery jQuery is one of many available libraries that – Provide functions for manipulating the web page With fairly good performance – Help.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
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.
Chapter 6 Murach's JavaScript and jQuery, C6© 2012, Mike Murach & Associates, Inc.Slide 1.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Week 3: Introduction to jQuery and Adv. Javascript.
JQUERY AND AJAX
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
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)
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
CSCI 3100 Tutorial 5 JavaScript & Ajax Jichuan Zeng Department of Computer Science and Engineering The Chinese University of Hong.
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.
Introduction to.
Programming Web Pages with JavaScript
Lecture 7: The Document Object Model (DOM); Unobtrusive JavaScript
Not a Language but a series of techniques
Intro to JavaScript CS 1150 Spring 2017.
Lecture 9: Events and timers
14 A Brief Look at JavaScript and jQuery.
Lecture 9: TIMERS and The DoM tree
Lecture 7: Unobtrusive JavaScript
CSc 337 Lecture 9: The DoM tree.
Lecture 8: Events and timers
Lecture 7: Unobtrusive JavaScript
Web Programming and Design
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

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!