Joseph Woyshner
DOM LEVELS
Event handling
Various browsers across the era of WWW
have implemented the DOM in different ways.
W3C group offers governance which provides a body of standards
for modern browsers to adhere to
preventDefault: function() {
var e = this.originalEvent;
this.isDefaultPrevented = returnTrue;
if ( !e ) {
return;
}
// If preventDefault exists, run it on the original event
if ( e.preventDefault ) {
e.preventDefault();
// Support: IE
// Otherwise set the returnValue property of the original event to false
} else {
e.returnValue = false;
}
jQuery handling various browser implementations for
preventing default event propagation.
window.document === document
> true
The document is at the top of the DOM hierarchy (root object)
Each webpage, or tab, has its own window object.
The window is actually part of the BOM
which interfaces with the DOM.
WEB DEVELOPMENT
Manipulating the DOM in Javascript is relatively expensive in comparison to other actions.
Scroll sideways!
$element = $('.myClass');
$element.html("hello world");
$element.css("color":"red");
$element
.html("hello world")
.css("color","red");
BAD
$.each(json_payload, function(){
$('table tbody').append(
'<tr>' +
'<td>'+this.name+'</td>'
'</tr>'
);
});
GOOD
var $reference = $('table tbody');
var htmlToInsert = "";
$.each(json_payload, function(){
$htmlToInsert +=
'<tr>' +
'<td>'+this.name+'</td>'
'</tr>'
);
});
$reference.append(htmlToInsert);
BAD
$('tr button').click(function(event){
do something...
});
iterates over each tr and applies click event handler. not efficient
$('tr ').on('click','button',function(event){
do something...
});
GOOD
tr is the reference container