The Misconceived Web The original vision of the WWW was as a hyperlinked document-retrieval system. It did.

Slides:



Advertisements
Similar presentations
Web 2.0 Programming 1 © Tongji University, Computer Science and Technology. Web Web Programming Technology 2012.
Advertisements

The JavaScript Programming Language
Building Fast 3rd-Party Webapps O'Reilly Velocity Web Performance and Operations Conference 24 June Lessons.
JavaScript: A Language of Many Contrasts
The Document Object Model
Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
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.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
The Web Warrior Guide to Web Design Technologies
© 2006 Douglas Crockford. The Misconceived Web The original vision of the WWW was as a hyperlinked document-retrieval system. It did not anticipate presentation,
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
HTML 1 Introduction to HTML. 2 Objectives Describe the Internet and its associated key terms Describe the World Wide Web and its associated key terms.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Unobtrusive JavaScript
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
DOM and JavaScript Aryo Pinandito.
JavaScript Tabriz university Its September 1995.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Creating an Animated Web Page
Working with Objects Creating a Dynamic Web Page.
XP Tutorial 4 New Perspectives on JavaScript, Comprehensive1 Working with Objects Creating an Animated Web Page.
Client side web programming Introduction Jaana Holvikivi, DSc. School of ICT.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
An Introduction to JavaScript Summarized from Chapter 6 of “Web Programming: Building Internet Applications”, 3 rd Edition.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
DHTML - Introduction Chapter Introduction to DHTML, the DOM, JS review.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
Modified from Moseley ’s sli desWeb Applications Development. Lecture 3 Slide 1 Lecture 3: Dynamic Web Pages – Introduction to JavaScript Instructor: Dr.
Client-side processing in JavaScript.... JavaScript history Motivations –lack of “dynamic content” on web pages animations etc user-customised displays.
JavaScript Tutorial 1 - Introduction to JavaScript1 Tutorial 1 Introduction to JavaScript Section A – Programming, HTML, and JavaScript.
An Introduction to JavaScript By: John Coliton Tuesday, November 10, 1998 Center for Teaching and Learning.
Cs332a_chapt10.ppt CS332A Advanced HTML Programming DHTML Dynamic Hypertext Markup Language A term describing a series of technologies Not a stand-a-lone.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
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.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
JavaScript Introduction. Slide 2 Lecture Overview JavaScript background The purpose of JavaScript A first JavaScript example Introduction to getElementById.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
Introduction to.
DHTML.
Chapter 1 Introduction to HTML.
Web Development & Design Foundations with HTML5 7th Edition
Document Object Model (DOM): Objects and Collections
JavaScript Introduction
Introduction to Programming the WWW I
Introduction to DHTML, the DOM, JS review
JavaScript CS 4640 Programming Languages for Web Applications
Chengyu Sun California State University, Los Angeles
Presentation transcript:

The Misconceived Web The original vision of the WWW was as a hyperlinked document-retrieval system. It did not anticipate presentation, session, or interactivity. If the WWW were still consistent with TBL's original vision, Yahoo would still be two guys in a trailer.

How We Got Here Rule Breaking Corporate Warfare Extreme Time Pressure

The Miracle It works! Java didn't. Nor did a lot of other stuff.

The Scripted Browser Introduced in Netscape Navigator 2 (1995) Eclipsed by Java Applets Later Became the Frontline of the Browser War Dynamic HTML Document Object Model (DOM)

Proprietary Traps Netscape and LiveWire Microsoft and Internet Information Services Both server strategies frustrated by Apache Browser-dependent sites

Pax Microsoft In the years since the end of the Browser War, the number of browser variations in significant use fell off significantly. W3C attempts to unify. Mozilla abandoned the Netscape layer model in favor of the W3C model. The browser platform becomes somewhat stable. DHTML becomes known as Ajax.

Browser

Scripted Browser

The World's Most Misunderstood Programming Language

Sources of Misunderstanding The Name Mispositioning Design Errors Bad Implementations The Browser Bad Books Substandard Standard JavaScript is a Functional Language

History 1992 Oak, Gosling at Sun & FirstPerson 1995 HotJava LiveScript, Eich at Netscape 1996 JScript at Microsoft 1998 ECMAScript

Key Ideas Load and go delivery Loose typing Objects as general containers Prototypal inheritance Lambda Linkage though global variables

Prototypal Inheritance function object(o) { function F() {} F.prototype = o; return new F(); } var myNewObj = object(myOldObj);

Pseudoclassical Inheritance function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; };

Parasitic Inheritance function gizmo(id) { return { id: id, toString: function () { return "gizmo " + this.id; } }; } function hoozit(id) { var that = gizmo(id); that.test = function (testid) { return testid === this.id; }; return that; }

Private function gizmo(id) { return { toString: function () { return "gizmo " + id; } }; } function hoozit(id) { var that = gizmo(id); that.test = function (testid) { return testid === id; }; return that; }

Shared Secrets function gizmo(id, secret) { secret = secret || {}; secret.id = id; return { toString: function () { return "gizmo " + secret.id; }; } function hoozit(id) { var secret = {}, /*final*/ that = gizmo(id, secret); that.test = function (testid) { return testid === secret.id; }; return that; }

Super Methods function hoozit(id) { var secret = {}, that = gizmo(id, secret), super_toString = that.toString; that.test = function (testid) { return testid === secret.id; }; that.toString = function () { return super_toString.apply(that, []); }; return that; }

Inheritance Patterns Prototypal Inheritance works really well with public methods. Parasitic Inheritance works really well with privileged and private and public methods. Pseudoclassical Inheritance for elderly programmers who are old and set in their ways.

Working with the Grain Pseudoclassical patterns are less effective than prototypal patterns or parasitic patterns. Formal classes are not needed for reuse or extension. Be shallow. Deep hierarchies are not effective.

JSLint JSLint can help improve the robustness and portability of your programs. It enforces style rules. It can spot some errors that are very difficult to find in debugging. It can help eliminate implied globals. Currently available on the web and as a Konfabulator widget. Soon, in text editors and Eclipse.

Warning: JSLint will hurt your feelings.

Hack for Mosaic and Navigator 1.0. language=javascript Deprecated. src= URL Highly recommended. Don't put code on pages. type=text/javascript Ignored.

Script files can have a big impact on page loading time. 1.Place tags as close to the bottom of the body as possible. (Also, place CSS as high in the head as possible.) 2.Minify and gzip script files. 3.Reduce the number of script files as much as possible. 4.Cache.

document.write Allows JavaScript to produce HTML text. Before onload: Inserts HTML text into the document. After onload: Uses HTML text to replace the current document. Not recommended.

Document Tree Structure document document.body document. documentElement

child, sibling, parent

Walk the DOM Using recursion, follow the firstChild node, and then the nextSibling nodes. function walkTheDOM(node, func) { func(node); node = node.firstChild; while (node) { walkTheDOM(node, func); node = node.nextSibling; }

getElementsByClassName function getElementsByClassName(className, node) { var results = []; walkTheDOM(node || document.body, function (node) { var a, c = node.className, i; if (c) { a = c.split(' '); for (i = 0; i < a.length; i += 1) { if (a[i] === className) { results.push(node); break; } }); return results; }

Manipulating Elements Old School if (my_image.complete) { my_image.src = superurl; } New School if (my_image.getAttribute('complete')) { my_image.setAttribute('src', superurl); }

Style node.className node.style.stylename node.currentStyle.stylename Only IE document.defaultView(). getComputedStyle(node, ""). getPropertyValue(stylename);

Style Names CSS background-color border-radius font-size list-style-type word-spacing z-index JavaScript backgroundColor borderRadius fontSize listStyleType wordSpacing zIndex

Making Elements document.createElement(tagName) document.createTextNode(text) node.cloneNode() Clone an individual element. node.cloneNode(true) Clone an element and all of its descendents. The new nodes are not connected to the document.

Linking Elements node.appendChild(new) node.insertBefore(new, sibling) node.replaceChild(new, old) old.parentNode.replaceChild(new, old)

Removing Elements node.removeChild(old) It returns the node. Be sure to remove any event handlers. old.parentNode.removeChild(old)

innerHTML The W3C standard does not provide access to the HTML parser. All A browsers implement Microsoft's innerHTML property.

Which Way Is Better? It is better to build or clone elements and append them to the document? Or is it better to compile an HTML text and use innerHTML to realize it? Favor clean code and easy maintenance. Favor performance only in extreme cases.

Events The browser has an event-driven, single-threaded, asynchronous programming model. Events are targeted to particular nodes. Events cause the invocation of event handler functions.

Event Handlers Classic node["on" + type] = f; Microsoft node.attachEvent("on" + type, f); W3C node.addEventListener(type, f, false);

Event Handlers The handler takes an optional event parameter. Microsoft does not send an event parameter, use the global event object instead.

Event Handlers function (e) { e = e || event; var target = e.target || e.srcElement;... }

Trickling and Bubbling Trickling is an event capturing pattern which provides compatibility with the Netscape 4 model. Avoid it. Bubbling means that the event is given to the target, and then its parent, and then its parent, and so on until the event is canceled.

Why Bubble? Suppose you have 100 draggable objects. You could attach 100 sets of event handlers to those objects. Or you could attach one set of event handlers to the container of the 100 objects.

Cancel Bubbling Cancel bubbling to keep the parent nodes from seeing the event. e.cancelBubble = true; if (e.stopPropagation) { e.stopPropagation(); } Or you can use YUI's cancelBubble method.

Prevent Default Action An event handler can prevent a browser action associated with the event (such as submitting a form). e.returnValue = false; if (e.preventDefault) { e.preventDefault(); } return false; Or you can use YUI's preventDefault method.

Memory Leaks Memory management is automatic. It is possible to hang on to too much state, preventing it from being garbage collected.

Memory Leaks on IE 6 Explicitly remove all of your event handlers from nodes before you discard them. The IE6 DOM uses a reference counting garbage collector. Reference counting is not able to reclaim cyclical structures. You must break the cycles yourself.

Memory Leaks on IE 6 That was not an issue for page view-driven applications. It is a showstopper for Ajax applications. It will be fixed in IE7.

Memory Leaks on IE 6 Remove all event handlers from deleted DOM nodes. It must be done on nodes before removeChild or replaceChild. It must be done on nodes before they are replaced by changing innerHTML.

Breaking Links in the DOM function purgeEventHandlers(node) { walkTheDOM(node, function (e) { for (var n in e) { if (typeof e[n] === 'function') { e[n] = null; } }); } Or you can use YUI's purgeElement method.

window The window object is also the JavaScript global object. Every window, frame, and iframe has its own unique window object. aka self. And sometimes parent and top.

Inter-window A script can access another window if It can get a reference to it. document.domain === otherwindow.document.domain Same Origin Policy

Cross Browser Weak standards result in significant vendor-specific differences between browsers. Graded Browser Support Browser Detection. Feature Detection. Platform Libraries.

The A List Firefox 1.0 Firefox 1.5 Mozilla 1.7 Safari 2 IE 5.5 IE 6 IE 7 Netscape 8 Opera 9

Browser Detection Determine what kind of browser that page is running in. Execute conditionally. Brittle. Not recommended. The browsers lie. navigator.userAgent Mozilla/4.0

Mozilla Mozilla/5.0

Firefox 1.5 Mozilla/5.0

Safari 2.0 Mozilla/5.0

IE 6.0 Mozilla/4.0

Opera 9.0

Feature Detection Using reflection, ask if desired features are present. Execute conditionally. function addEventHandler(node, type, f) { if (node.addEventListener) { node.addEventListener(type, f, false); } else if (node.attachEvent) { node.attachEvent("on" + type, f); } else { node["on" + type] = f; }

Feature Detection Using reflection, ask if desired features are present. Execute conditionally. function addEventHandler(node, type, f) { node["on" + type] = f; } YAHOO.util.Event.addListener(node, type, f); Support for custom events, and for adding events to object that don't exist yet, and for purging event handlers from objects.

Use a Platform Library A platform library insolates the application from the poisonous browsers. YUI is highly recommended.

The Cracks of DOM The DOM buglist includes all of the bugs in the browser. The DOM buglist includes all of the bugs in all supported browsers. No DOM completely implements the standards. Much of the DOM is not described in any standard.

Coping 1.Do what works. 2.Do what is common. 3.Do what is standard.

The Wall Browsers are now being push to their limits. Be prepared to back off. Reduce your memory requirements. Balance of client and server.

The Hole The browser was not designed to be a general purpose application platform. Lacks a compositing model. Accessibility suffers. Lacks support for cooperation under mutual suspicion.

The Peace is ending.

WWW War II Microsoft has awoken. They are beginning to innovate again. There are now 4 major browser makers. They will be flooding the web with bugs.

ECMAScript The Fourth Edition is in development. Most of the new features are syntax errors in the current browsers. It will be years before we can use them. We will have to deal with the new bugs an incompatibilities immediately. The sad case of E4X.

We Will Prevail We must use our clout to keep the browser makers and standards bodies in line. We must be players, not pawns. Ultimately, the development community must set the standards.