Lecture from http://video.yahoo.com/video/play?vid=111582
http://yuiblog.com/assets/crockford/theory.zip
http://yuiblog.com/assets/crockford/theory.zip
Browser is source of pain and misery. Can't talk about all the quirks. Talk about the theory of the DOM.
Jesse James Garrett named DHTML AJAX during Pax Microsoft. The browser became stable enough to think of it as a application platform.
URL Flow Paint. FF 1.5 2.0, safari 2.0, IE 5.5, 6, 7, NS 8, Opera 9 "The A List" browsers.
hack for mosaic and navigator 1.0, so it's not needed anymore. Not necessary for 10 years. language=javascript is deprecated. type=text/javascript ignored (problem: MIME type wasn't issued till this year, ignored when loading from source URL). src=URL is very good, added in Navigator 3.
Put script src tags as low in the body of the page as possible. Put CSS as high in head as possible.
Gzip javascript. take advantage of cache headers. For a while, put javascript in one file until widely deployed.
Document.write not recommended. Better alternatives. Sensitive to when you call it. If you call before onload, it's fine, calling after it destroys document.
Lots of collections defined; don't use them either. document.anchors, .applets, .embeds, .forms, .frames, .images, .plugins, .scripts, .stylesheets.
name vs id: used be interchangable, but now use name for form data. used to correlate radio buttons, name window or frame. id uniquely identifies elements to get access.
avoid document.all. Introduced by Microsoft; rejected by W3C.
use document.getElementById(id), .getElementsByName(name), .getElementsByTagName(tag) [can call on any node]
Node or tag names retrieved may be in upper case. Browser will add document and head nodes even if not present. IE and Firefox create a different tree because W3C requires whitespace to be captured. (Which sucks but it's a standard, except for IE).
You have complex pointers, but you only need a subset. You can get by with firstChild, nextSibling:
function walkTheDOM(node, func){
func(node);
while(node){
walkTheDOM(node, func);
node = node.nextSibling
}
}
w3c forgot getElementsByClassName - you can use the above for that. To access class attribute, use .className because class is a reserved word.
Part 2
manipulating elements
Event Model
events single threaded async
always targeted to a node (the top most node containing the cursor)
take an optional event object. MS puts the event in the global namespace. Use this boilerplate to deal with it:
handler(e){
e= e || event; //if e is falsy, get it from global namespace
var target = e.target || e.srcElement;
}
Trickling and Bubbling - how events propogate
memory management
walkTheDOM(node, function(e) {
for (var n in e) {
if (typeof e[n] === 'function') {
e[n] = null;
}
}
)};
}(walk the dom defined elsewhere)
JavaScript features
Do what is common.
Do what is standard.
The wall
- Rule breaking
- Corporate warfare
- Extreme Time pressure
Jesse James Garrett named DHTML AJAX during Pax Microsoft. The browser became stable enough to think of it as a application platform.
URL Flow Paint. FF 1.5 2.0, safari 2.0, IE 5.5, 6, 7, NS 8, Opera 9 "The A List" browsers.
hack for mosaic and navigator 1.0, so it's not needed anymore. Not necessary for 10 years. language=javascript is deprecated. type=text/javascript ignored (problem: MIME type wasn't issued till this year, ignored when loading from source URL). src=URL is very good, added in Navigator 3.
Put script src tags as low in the body of the page as possible. Put CSS as high in head as possible.
Gzip javascript. take advantage of cache headers. For a while, put javascript in one file until widely deployed.
Document.write not recommended. Better alternatives. Sensitive to when you call it. If you call before onload, it's fine, calling after it destroys document.
Lots of collections defined; don't use them either. document.anchors, .applets, .embeds, .forms, .frames, .images, .plugins, .scripts, .stylesheets.
name vs id: used be interchangable, but now use name for form data. used to correlate radio buttons, name window or frame. id uniquely identifies elements to get access.
avoid document.all. Introduced by Microsoft; rejected by W3C.
use document.getElementById(id), .getElementsByName(name), .getElementsByTagName(tag) [can call on any node]
Node or tag names retrieved may be in upper case. Browser will add document and head nodes even if not present. IE and Firefox create a different tree because W3C requires whitespace to be captured. (Which sucks but it's a standard, except for IE).
You have complex pointers, but you only need a subset. You can get by with firstChild, nextSibling:
function walkTheDOM(node, func){
func(node);
while(node){
walkTheDOM(node, func);
node = node.nextSibling
}
}
w3c forgot getElementsByClassName - you can use the above for that. To access class attribute, use .className because class is a reserved word.
Part 2
manipulating elements
- old school: img.src = 'someurl'; - smaller and faster, probably preferred.
- new school img.setAttribute('src', 'someurl'); - recommended by w3c; set has side effect, so that's nice.
- node.className = 'myClass'; //note can have multiple classes with space between.
- node.style.mystyle
- to get css style: IE only: node.currentStyle. w3c: document.defaultView().getComputedStyle(mynode, "").getPropertyValue('mystyle');
- to read style info you have to do it both ways. Ugh.
- unfortunate naming conventions: css properties are camel case. "should have been designed better."
- document.createElement(tagName)
- document.createTextNode(text)
- node.cloneNode()
- node.cloneNode(true) - include all descendants (Q: is there ever a time that you can't do this?)
- new nodes aren't connected to the document yet.
- node.appendChild(newNode)
- node.insertBefore(newNode, sibling)
- node.replaceChild(newNode, oldNode)
- old.parentNode.replaceChild(new, old) - weird that you have to specify old twice! w3c screwup - or at least doug doesn't agree with it. :)
- old.parentnode.removeChild(old) - returns the old node
- make sure you remove all event handlers from the oldfirst.
- w3c doesn't provide access to html parser [q: what does this mean? they wanted you to write an html parser and manipulate the dom directly]
- Microsoft defined the de facto standard, innerHTML property that all browsers support. Set html here and the parser will insert it into the tree.
Event Model
events single threaded async
always targeted to a node (the top most node containing the cursor)
- click
- dblclick
- mousedown
- mousemove
- mouseout
- mouseover
- mouseup
- blur
- change
- focus
- keydown
- keypress
- keyup
- reset
- submit
- node.["on" + type] = f; works on all browsers
- node.attachEvent("on" + type, f); MS only
- node.addEventListener(type, f, false); w3c - note that type doesn't have "on" in front. last parm is always false
take an optional event object. MS puts the event in the global namespace. Use this boilerplate to deal with it:
handler(e){
e= e || event; //if e is falsy, get it from global namespace
var target = e.target || e.srcElement;
}
Trickling and Bubbling - how events propogate
- trickle: events go from the top node and go down, letting nodes respond
- bubble: events go from bottom and up to the top.
- w3c does both - first trickle down, then bubble up. that's what the 3rd parm defines.
- why bubble? you can attach event listener to parent!!! vs attaching to a bunch of elts.
- it doesn't stop bubbling - the handler has to be explicit
- e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation();
- or use a platform library
- after the handler finishes the browser takes default action unless you tell it otherwise
- e.returnValue = false; if (e.preventDefault) e.preventDefault();
- or use a platform library
memory management
- auto garbage collected
- possible to hold onto too much state - programmatic error. set to null to let it release.
- biggest leak in IE6: explicitly remove event handlers before remove them from the DOM. IE6 uses reference counting GC. (that doesn't work with cycles). Didn't show up for a long time because not much script was running and long running pages on the screen. Fixed in IE7. do before removeChild or removeChildren and innerHTML
walkTheDOM(node, function(e) {
for (var n in e) {
if (typeof e[n] === 'function') {
e[n] = null;
}
}
)};
}(walk the dom defined elsewhere)
JavaScript features
- alert - don't use in ajax - it blocks the browser thread. use platform library instead
- confirm -
- prompt
- setTimeout, set interval
- window object is the JavaScript global object
- every window, frame, iframe has unique window object.
- aka self, parent, top.
- frames[] - child frames and iframes
- name - text name of a window
- opener - reference to open
- parent
- self - reference to this window [what does that mean? from the perspective of the script?]
- top - reference to outermost window
- window - reference to this window
- open() - open a new window - sometimes work but sometimes doesn't. this is what popup blockers block
- can access another window if it can get a reference to it e.g. document.domain === otherwindow.document.domain (won't work with subdomains, unless both scripts shorten their domain to the TLD only)
- same origin policy
- Browser detection - not recommended because browsers lie; brittle. (see PPK)
- Feature detection - use reflection capabilities
- Platform libraries - YAHOO.util.Event developer.yahoo.com/yui
- 200 ajax libraries or so, comprehensive minimal, small large team
- Doug guesses there will be two winners: MS Atlas (strongest toolset, documentation, support), YUI could be the other winner (minimal, but extensible, best documented, free).
- why? real need for api for applications, fun to make
- Writing to multiple buglists
- DOM isn't described fully anywhere - w3c specs aren't complete
- "skating across 5 layers of cracked ice"
Do what is common.
Do what is standard.
The wall
- browsers getting pushed ot limit: memory, balance of client and server. Photo3d hit the wall.
- not designed to be app platform:
- lacks compositing model;
- accessibility suffers;
- lacks support for cooperation under mutual suspicion. Cannot protect mashup components from each other. Be careful about mixing private data.
No comments:
Post a Comment