Professional Documents
Culture Documents
Hackday
Hackday
ppt
The Misconceived Web
• The original vision of the WWW was as a
hyperlinked document-retrieval system.
• Corporate Warfare
• Java didn't.
• Dynamic HTML
• 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.
Display
URL Cache Tree Pixels
List
Scripted Browser
Flow Paint
Script Event
Script
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.
http://www.JSLint.com/
<script></script>
• <!-- // -->
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></script>
• Script files can have a big impact on page
loading time.
4. Cache.
document.write
• Allows JavaScript to produce HTML
text.
• Not recommended.
Document Tree Structure
document #document
<html>
<body>
HTML <h1>Heading 1</h1>
document.
<p>Paragraph.</p>
documentElement HEAD
<h2>Heading 2</h2>
<p>Paragraph.</p>
document.body BODY </body>
</html>
H1
#text
P
#text
H2
#text
P
#text
firstChild firstChild
H1
#text
BODY
lastChild
firstChild
lastChild
#text
lastChild
firstChild
H2
#text
lastChild
firstChild
child, sibling, parent
#text
lastChild
child, sibling, parent
BODY
lastChild
firstChild
firstChild
firstChild
firstChild
lastChild
lastChild
lastChild
lastChild
BODY
lastChild
parentNode
firstChild
parentNode
parentNode
parentNode
firstChild
firstChild
firstChild
firstChild
lastChild
lastChild
lastChild
lastChild
BODY
firstChild
firstChild
firstChild
firstChild
#text #text #text #text
Walk the DOM
• Using recursion, follow the firstChild
node, and then the nextSibling nodes.
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 JavaScript
• background-color • backgroundColor
• border-radius • borderRadius
• font-size • fontSize
• list-style-type • listStyleType
• word-spacing • wordSpacing
• z-index • 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.
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 Parse
• 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.
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
e.returnValue = false;
if (e.preventDefault) {
e.preventDefault();
}
return false;
• 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
• http://developer.yahoo.com/yui/articles/gbs/gbs_browser-chart.html
Browser Detection
• Determine what kind of browser that page
is running in.
• Execute conditionally.
• http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
Mozilla 1.7.13
Mozilla/5.0
Firefox 1.5
Mozilla/5.0
Safari 2.0
Mozilla/5.0
IE 6.0
Mozilla/4.0
Opera 9.0
Opera 9.0
Feature Detection
• Using reflection, ask if desired features are present.
• Execute conditionally.
• Execute conditionally.
• http://developer.yahoo.com/yui/
The Cracks of DOM
• The DOM buglist includes all of the bugs
in the browser.
2. Do what is common.
3. Do what is standard.
The Wall
• Browsers are now being push to
their limits.
• Accessibility suffers.