zoukankan      html  css  js  c++  java
  • js客户端检测

    1.能力检测的基本模式如下:

    //能力检测的基本模式
    if (object.propertyInQuestion) {
    // 使用object.propertyInQuestion
    }
    
    throw new Error("No way to retrieve element")
    
    // 如检查sort是不是函数(存在)
    fucntion isSortable(object) {
    return typeof object.sort == "function";
    }

    在可能情况下,尽量使用typeof 进行能力检测

    // 检测所有浏览器的Flash
    function hasFlash() {
    var result = hasPlugin('Flash');
    if (!result) {
    result = hasIEPlugin('ShockwaveFlash.ShockwaveFlash');
    }
    return result;
    }
    
    // 检测Flash
    // alert(hasFlash());

    对于方法检测

    // 如检查sort是不是函数(存在)
    function isSortable(object) {
    return typeof object.sort == "function";
    }
    
    // 在浏览器环境下测试任何对象的某个特性是否存在
    /**
    * author:Peter Michaux
    */
    function isHostMethod(object, property) {
    var t = typeof object[property];
    return t == "function" || (!!(t == 'object' && object[property])) || t == "unknown";
    }
    
     
    
    //确定浏览器是否支持Netscape风格的插件
    var hasNSplugins = !!(navigator.plugins && navigator.plugins.length);
    
    // 确定浏览器是否具有DOM1级规定的能力
    var hasDOM1 = !!(document.getElementById && document.createElement && docuemnt.getElementByTagName);
    
    
    // 怪癖检测
    var hasDontEnumQuick = function() {
    var o = { toString: function() {} };
    for (var prop in o) {
    if (prop == "toString") {
    return false;
    }
    }
    return true;
    }();

    用户代理检测是客户端检测的最后一个选择,优先使用能力检测和怪癖检测

  • 相关阅读:
    yii2 批量插入
    yii2 ArrayHelper的19个函数+使用实例+功能详解
    thinkphp phpexcel
    yii2 ActiveForm beforeSubmit用法
    YII2项目常用技能知识总结
    Redis 的 fields 遇到的问题
    spring boot +mybatis+druid 多数据源配置
    简单使用shell 自动打包,发布项目 脚本
    cmpp 短信平台
    mysql workbench 导出表结构
  • 原文地址:https://www.cnblogs.com/sundjly/p/7899037.html
Copyright © 2011-2022 走看看