Summary of main event points in Chapter 12th of javascript advanced programming (common cross-browser detection methods)

Source: Internet
Author: User

Copy codeThe Code is as follows:
Var EventUtil = {// cross-browser handler --- Creation Method
AddHandler: function (element, type, handler ){
If (element. addEventListener ){
Element. addEventListneter (type, handler, false );
} Else if (element. attachEvent ){
Element. attachEvent ("on" + type, handler );
} Else {
Element ["on" + type] = handler;
}
}
RemoveHandler: function (element, type, handler) {// cross-browser handler --- Delete Method
If (element. removeEventListener ){
Element. removeEventListneter (type, handler, false );
} Else if (element. detachEvent ){
Element. detachEvent ("on" + type, handler );
} Else {
Element ["on" + type] = handler;
}
}
GetEvent: function (event) {// cross-browser event object --- return the reference of the event object
Return event? Event: window. event;
}
GetTarget: function (event) {// cross-browser event object --- return the event Target
Return event.tar get | event. srcElement;
}
PreventDefault: function (event) {// cross-browser event object --- cancel default event
If (event. preventDefault ){
Event. preventDefault ();
} Else {
Event. returnValue = false;
}
}
Stoppropagation: function (event) {// cross-browser event object --- block event stream
If (event. stoppropagation ){
Event. stoppropagation ();
} Else {
Event. canceBubble = false;
}
}
GetRelatedTarget: function (event) {// cross-browser retrieval of Related Elements
If (event. relatedTarget ){
Return event. relatedTarget;
} Else if (event. toElement ){
Return envent. toElement;
} Else if (event. fromElement ){
Return event. fromElement;
} Else {
Return null;
}
}
GetButton: function (event) {// button attribute detection for mouse events
If (document. implementation. hasFeature ("MouseEvent", "2.0 ")){
Return event. button;
} Else {
Switch (event. button ){
Case 0:
Case 1:
Case 3:
Case 5:
Case 7:
Return 0;
Case 2:
Case 6:
Return 2
Case 4:
Return 1
}
}
}
GetCharCode: function (event) {// cross-browser character encoding --- charCode attribute Detection
If (typeof event, charCode = "number "){
Return event. charCode;
} Else {
Return event. keyCode;
}
}
}

Event delegation: a solution with too many Event Handlers reduces memory and improves performance;
Simulated Event: This is complicated and requires further research;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.