惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
腾讯CDC
P
Privacy & Cybersecurity Law Blog
V
Vulnerabilities – Threatpost
小众软件
小众软件
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
P
Privacy International News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
Forbes - Security
Forbes - Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
Google Developers Blog
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
TaoSecurity Blog
TaoSecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Last Week in AI
Last Week in AI
H
Heimdal Security Blog
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
Security Latest
Security Latest
SecWiki News
SecWiki News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Security Affairs
V2EX - 技术
V2EX - 技术
S
Schneier on Security
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园_首页
AI
AI
Help Net Security
Help Net Security
I
Intezer
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
D
Docker

博客园 - 令狐葱★

【备忘】12306购票必杀技 Github上最受关注的前端大牛,快来膜拜吧! Javascript知识点:IIFE - 立即调用函数 那些开源程序中让人叹为观止的代码 - 3 保持元素纵横比 那些开源程序中让人叹为观止的代码 - 2 单例模式 浏览器中F5和CTRL F5的行为区别 CR, LF, CR/LF区别与关系 利用 jQuery 克隆 Object Chrome下Failed to load resource问题居然是由于AdBlock WordPress在线安装插件 jQuery中的.height()、.innerHeight()和.outerHeight() jQuery中的attr()和prop() jQuery中的事件绑定函数.bind()、.live()、on()和.delegate() 实现ECMAScript 5中的Object.create()函数和Object.getPrototypeOf() 函数 Javascript全局变量和delete jQuery中$.proxy()的原理和使用 Javascript基类对象原型中有数组的情况 必须关注的25位知名JavaScript开发者 大坑贴:CSS浏览器兼容性问题及bug汇总
那些开源程序中让人叹为观止的代码 - 1 浏览器特性判断
令狐葱★ · 2014-04-10 · via 博客园 - 令狐葱★

浏览器特性判断

解决问题:判断某事件/方法在当前浏览器中是否支持

开源程序:Modernizr

众所周知,各个不同的浏览器对于代码渲染的实现也各自为政百花齐放,虽然有W3C在维护着标准,但是由于市面上存在很多浏览器,每个浏览器又有数不尽的历史版本,因此对于浏览器兼容性问题的判断成了在写JS应用中必不可少的技能。

以前大家都倾向于做浏览器类型和版本的检测,现在倾向于做浏览器的特性检测,这样更有实际用处。今天要提到的是通过判断事件是否存在来探测浏览器兼容性支持。比如onmousewheel事件,并不是所有的浏览器都会支持,我们可以这样:

if(document.onmousewheel){
    //这里是处理代码
}

这种方式会有一个明显的弊端,如果某恶意用户注入了一段代码,重新定义了document.onmousewheel事件的实现,那么就会偏离我们的预期,执行到了不该执行的代码。

看看Modernizr中是如何来做的。

function is( obj, type ) {
    return typeof obj === type;
}

var isEventSupported = (function() {

  var TAGNAMES = {
    'select': 'input', 'change': 'input',
    'submit': 'form', 'reset': 'form',
    'error': 'img', 'load': 'img', 'abort': 'img'
  };

  function isEventSupported( eventName, element ) {

    element = element || document.createElement(TAGNAMES[eventName] || 'div');
    eventName = 'on' + eventName;

    var isSupported = eventName in element;

    if ( !isSupported ) {
      if ( !element.setAttribute ) {
        element = document.createElement('div');
      }
      if ( element.setAttribute && element.removeAttribute ) {
        element.setAttribute(eventName, '');
        isSupported = is(element[eventName], 'function');

        if ( !is(element[eventName], 'undefined') ) {
          element[eventName] = undefined;
        }
        element.removeAttribute(eventName);
      }
    }

    element = null;
    return isSupported;
  }
  return isEventSupported;
})();

使用方式也很简单:

isEventSupported("mousewheel")// Chrome
-> true 
isEventSupported("mousewheel")// Firefox
-> false