Common Native JS compatible notation

Source: Internet
Author: User

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

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.