In our front-end development, often encounter the problem of compatibility, because to consider the user will use a different browser to access your page, you have to ensure that your Web page in any browser can run normally, the following I will give a few common native JS compatible wording:
1: Add Event method
addHandler:function(element,type,handler){
if
(element.addEventListener){
//检测是否为DOM2级方法
element.addEventListener(type, handler,
false
);
}
else
if
(element.attachEvent){
//检测是否为IE级方法
element.attachEvent(
"on"
+ type, handler);
}
else
{
//检测是否为DOM0级方法
element[
"on"
+ type] = handler;
};
};2: Get event and event object targets
getEvent:
function
(event){
return
event ? event : window.event;
},
getTarget:
function
(event){
return
event.target || event.srcElement;
};3: Block compatibility of browser default events
preventDefault:
function
(event){
if
(event.preventDefault){
event.preventDefault();
}
else
{
event.returnValue =
false
;
};
};4: Compatibility notation for blocking event bubbling
stopPropagation:
function
(event){
if
(event.stopPropagation){
event.stopPropagation();
}
else
{
event.cancelBubble =
true
;
};
};5: Cross-browser way to get character encoding
getCharCode:
function
(event){
if
(
typeof
event.charCode ==
"number"
){
return
event.charCode;
}
else
{
return
event.keyCode;
};
};Compatibility issues in basic native JS I can think of it is so much, I hope that everyone's study is helpful. Add a CSS3 compatibility-webkit this prefix for the browser represents the private properties of the browser
-ms This is the prefix of IE browser
-moz this represents the Firefox prefix.
-O This represents the open gate browser
-webkit This represents the Google browser and the Apple browser is written here today, see you tomorrow! Hold on, hold on, hold on!
Common Native JS compatible notation