Академический Документы
Профессиональный Документы
Культура Документы
*****************************************
--> When an event happens, it flows from document to specific element first
(capturing phase) and then from specific element to document (bubbling phase).
Handler can be attached to any element in this path and for any phase.
addEventListener() takes a boolean (third parameter) to decide phase.
true ==> handler for capturing.
false ==> handler for bubbling.
EX:
If div1 contains div2 & div2 contains div3. All the three divs have click handler
for capturing phase & bubbling phase. When user clicks on div3, handler order:
div1 (capturing phase)
div2 (capturing phase)
div3 (capturing phase)
div3 (bubbling phase)
div2 (bubbling phase)
div1 (bubbling phase)
The main practical use of capturing and bubbling is the registration of default
handler.
document.onclick = doSomething;
jQuery live() works by attching handler to document and handles event in bubbling
phase.
***********************************************************************************
*****************************************
Why turn off bubbling?
The browser has to go through every single ancestor element of the event target to
see if it has an event handler.
Even if none are found, the search still takes time.
***********************************************************************************
*****************************************
event.type says whether event is click, keydown, keyup, keypress, change, focus or
blur.
event.target (non IE) or event.srcElement (IE) returns the element the event took
place on. This target or srcElement never changes during capturing or bubbling
phase. currentTarget property contains a reference to the HTML element the event is
currently being handled by:
In Safari, if event happens on element with text, event.target refers text node. To
get actual element, use following code:
var tgt = event.target;
if (targ.nodeType == 3) { targ = targ.parentNode; }
***********************************************************************************
*****************************************
event.button says which mouse button is clicked:
Left button ==> 0
Middle button ==> 1
Right button ==> 2
***********************************************************************************
*****************************************
Certain elements have default actions for click event. For example, anchor's click
action opens link given in href. form's submit button click sends data to server.
What if these alements also have click handlers? What is order of action?
a) the event handler script is executed first.
b) the default action takes place afterwards.
What if click handler is enough and default action is not needed? Use following
ways:
return false; // traditional
event.preventDefault(); // w3c model
event.returnValue = false; // IE model.
***********************************************************************************
***************************************
How to get event & element in onclick handler?
***********************************************************************************
***************************************
DOMSubtreeModified event fires on a node when a modification occurs in the subtree
that belongs to it.
ele.addEventListener("DOMSubtreeModified", handler, true);
***********************************************************************************
*****************************************
How to add functions to handle mouseout & mouseover?
function submitMouseOut {}
submitBtn.onmouseout = submitMouseOut;
submitBtn.onmouseover = submitMouseOver;
or
Note:
DOM's mouseover & mouseout will be called multiple times. Do not use them. Instead,
use jQuery's mouseenter & mouseleave which are called only once.
***********************************************************************************
*****************************************
How to get event object along with i/p parameters in an event handler?
return fnDelegate;
}
***********************************************************************************
*****************************************
How to use createDelegate for global or local functions?
function f()
{
var g = function(i, j)
{
};
***********************************************************************************
*****************************************
Safari event handling:
http://developer.apple.com/library/safari/#documentation/appleapplications/referenc
e/safariwebcontent/HandlingEvents/HandlingEvents.html
***********************************************************************************
*****************************************
gestureevent is for 2 or more fingers.
http://developer.apple.com/library/safari/#documentation/UserExperience/Reference/G
estureEventClassReference/GestureEvent/GestureEvent.html#//apple_ref/doc/uid/TP4000
9353
***********************************************************************************
*****************************************
How to get mouse position in an event handler?
(function() {
window.onmousemove = handleMouseMove;
function handleMouseMove(event) {
event = event || window.event; // IE-ism
// event.clientX and event.clientY contain the mouse position
}
})();
***********************************************************************************
*****************************************
document.activeElement property has the element which has focus.
***********************************************************************************
*****************************************
window.onscroll specifies the function to be called when the window is scrolled.
EX:
window.onscroll = function (oEvent) {};
***********************************************************************************
*****************************************
How to disable right click or browser context menu on an element?
How to show custom menu on right click?
$("#ele").mousedown(onMouseDown);
$(document).bind("contextmenu",function(e){
console.log('Context Menu event has fired. But browser context menu will not be
shown');
e.preventDefault();
return false;
});
Note:
Adding contextmenu on individual elements don't work.
***********************************************************************************
*****************************************
Special keys:
IE doesn't fire the keypress event for delete, end, enter, escape, function keys,
home, insert, pageUp/Down and tab
To detect these keys, search for their keyCode onkeydown/up, and ignore both
onkeypress and charCode.
http://www.quirksmode.org/js/keys.html
***********************************************************************************
*****************************************
How to register handler for custom or app specific event and trigger it? Bind
custom event handler to document and also listen from document. EX:
$(document).bind('custom.event', function(){ console.log("Received
custom.event"); });
$(document).trigger('custom.event');
***********************************************************************************
*****************************************