dojo/has提供了标准化的特征检测。 它是基于 has.js 项目(约定了方法签名,比如 has方法,有哪些参数,参数顺序等,相当于提供了一个标准,大家都按这个标准去做的话,那么特征检测
dojo/has提供了标准化的特征检测。 它是基于 has.js 项目(约定了方法签名,比如 has方法,有哪些参数,参数顺序等,相当于提供了一个标准,大家都按这个标准去做的话,那么特征检测在所有的库中就可以通用了)
介绍
源文档:http://dojotoolkit.org/reference-guide/1.9/dojo/has.html
在客户端代码里使用浏览器的user-agent检测 或者特征检测都有各自的缺陷。dojo/has API先添加新的特征测试代码,在需要检测某个特征时,在执行特征测试特码, 并缓存结果, 这句有点不好理解,可以使用代码来解释
has.add("dom",function(){return true}); //特征测试
if(has(dom){ // has(dom) 就是需要先检测某个特征。如果有缓存结果,直接调用缓存,如果没有,执行上面的测试代码
// do someting
}
dojo/has 模块主要是为dojo的模块提供标准的特征测试和特征检测。 虽然 dojo/has API 实现了 has.js 规范中的方法和 规定的特征名,但dojo模块用的是自己的特征检测。
dojo/has 特征检测是懒实例化, 什么意思呢? 就是特征测试代码只会在请求某个特征时才会运行,并且为之后相同的特征检测 缓存结果。
Dojo Core 和 Dijit 模块使用 dojo/has 做特征检测。 但还是有很多 DojoX 项目是继续使用dojo.isXXX 的浏览器代理 user agent侦测技术。 这是一个慢慢演进的过程。
dojo/has 可以作为一个插件,通过三元表达式(?:),实现有条件加载模块。
基本的测试是定义在dojo/has模块,在附加在相应模块上的特征和测试,可以增强相应模块的特征检测。 最常使用的 dojo/sniff 就是扩展 dojo/has, 并添加自己额外的测试。
使用staticHasFeatures 在编译配置里, 并结合 Google 的 closure 可以删除无效代码的路径。 查看 builder 文档获得更多的信息.
使用
为了使用这个模块, 它必须被添加到你模块的依赖列表里, 例如:
define(["dojo/has"], function(has){
if(has("dom")){
// Do something based on feature
}
});
特征检测一般会返回一个 "truthy"(Something which evaluates to TRUE, 看了下goole的解释,代表的这个值是需要评估的,这里主要是突出评估,对像,函数,数组都是truthy. 但比如特征检测用到的测试函数,函数就是一个truthy, 但这个测试函数可能会回返一个false的值。这样理解,不知道是对还是错。) 的值。意思是如果功能存在, 返回一个true, 如果不顾在,返回false.
这个模块可以被用于loader的插件, 这么做, 插件的参数是一个三元逻辑表达式, 第一个参数就是feature, 第二个是当 feature为true时要加载的模块,第三个是为false要加载的模块。
require(["dojo/has!feature?package/module:package/other"], function(featureModule){
// If feature is true, package/module loaded
// If feature is false, package/other loaded
});
注册一个特征检测需要提供特征检测的名称和测试函数:
has.add("some-test-name", function(global, document, anElement){
// Feature dection code, returning a truthy value if true
return true;
});
测试函数应该接受三个参数:
Argument |
Description |
global
引用全局作域
document
引用document对像
anElement
是否需要使用到某个HTML元素
注意, 任何测试函数都应该自行清理, 这样不会导致内存的溢出。
当你在测试函数后添加一个 true时,会立即运行测试函数。
has.add("some-other-test", function(){
return false; // Boolean
}, true);
虽然你可以如下,传递一个非封装的函数,但这是不被建议的:
// this is not wrapped in a function, and should be:
has.add("some-other-test", ("foo" in bar)); // or whatever
没有被函数封装, 它会立即执行,而不是之前的那样,在实际需要进行代码检测的路径里执行。
特征名称
以下的特征测试在Dojo中是有效的。 这个表显示了 哪些模块中有哪些特征测试和测试的名字。
Module |
Feature |
Description |
dojo/_base/browser
config-selectorEngine
Pre-configured selector engine to use in dojo/query, defaults to dojo/selector/acme.
dojo/_base/connect
events-keypress-typed
Keypresses should only occur a printable character is hit
dojo/_base/kernel
extend-dojo
Defined Dojo modules should push their definitions into the dojo object. In 2.0, it will likely be unusual to augment another object as a result of defining a module. This has feature gives a way to force 2.0 behavior as the code is migrated.
dojo/_base/kernel
dojo-guarantee-console
Ensure that console.log, console.warn, etc. methods are defined
dojo/_base/kernel
dojo-debug-messages
Log internal debug messages generated by Dojo, these include deprecated/experimental warnings along with parser auto-required module names.
dojo/_base/kernel
dojo-modulePaths
Consume module paths defined in config.modulePaths.
dojo/_base/kernel
dojo-moduleUrl
Expose dojo.moduleUrl method, returns a URL relative to a module. Deprecated in 2.0, should use require.toUrl()
dojo/_base/lang
bug-for-in-skips-shadowed
Test for bug where the for-in iterator skips object properties that exist in Object’s prototype (IE6 - ?).
dojo/_base/loader
dojo-fast-sync-require
All dojoRequireCallbacks can be released when all non-dojo/require!, dojo/loadInit! modules are either executed, not requested, or arrived. Potential weakness of this algorithm is that dojo/require will not execute callbacks untilall dependency trees are ready.
dojo/_base/loader
config-publishRequireResult
Publish resolved module values, resulting from a require call, as JavaScript objects referenced by module identifiers in the global namespace.
dojo/_base/window
quirks
Browser is running in Quirks-Mode
dojo/dojo
host-node
设置Dojo的运行环境在Nodejs平台
dojo/dojo
host-rhino
Dojo 的运行环境 on the Rhino 平台
dojo/dojo
dojo-xhr-factory
dojo/dojo
dojo-force-activex-xhr
Force XHR provider to use ActiveX API (MSXMLHTTP).
dojo/dojo
native-xhr
Browser has native XHR API, XMLHttpRequest.
dojo/dojo
dojo-gettext-api
Dojo provides API for retrieving text resource contents from a URL.
dojo/dojo
dojo-loader-eval-hint-url
Module location should be used as source hint during eval rather than module identifier.
dojo/dojo
ie-event-behavior
Browser supports legacy IE event behaviour API (attachEvent versus attachEventListener).
dojo/has
host-browser
Dojo 运行在浏览器平台
dojo/has
dom
当前平台支持 DOM
dojo/has
dojo-dom-ready-api
当前平台上支持 DOMReady API, 当DOM加载完时,会通知一个监听器, 代码在dojo.js
dojo/has
dojo-sniff
允许扫描dojo.js 角本中的 data-dojo-config 和 djConfig 标签
dojo/has
dom-addeventlistener
Standard DOM event API (addEventListener) supported on the current platform.
dojo/has
touch
Touch events are supported on the current platform
dojo/has
device-width
Amount of horizontal space in pixels available on the window
dojo/has
dom-attributes-explicit
DOM node attributes array only lists explicitly user specified attributes, (W3C standard)
dojo/has
dom-attributes-specified-flag
DOM node attribute values provide specified flag to skip attributes user didn’t specify, (IE8)
dojo/hccss
highcontrast
Browser is in ‘high-contrast’ mode
dojo/i18n
dojo-preload-i18n-Api
Define the preload localizations machinery, allow loading of special rollup modules which contain one or more flattened, localized bundles.
dojo/i18n
dojo-v1x-i18n-Api
Define legacy (v1.x) i18n functions
dojo/json
json-parse
Platform supports parsing JSON text to JavaScript objects through native API
dojo/json
json-stringify
Platform supports ‘stringify’ method on native JSON API, allowing serialisation of JavaScript objects to JSON text.
dojo/mouse
dom-quirks
Browser is running in Quirks-Mode
dojo/mouse
events-mouseenter
Browser supports the onmouseenter DOM event
dojo/mouse
events-mousewheel
Browser supports the onmousewheel DOM event
dojo/on
jscript
JavaScript environment provided by the JScript platform, dialect of ECMAScript standard that is used in Microsoft’s Internet Explorer.
dojo/on
event-orientationchange
Browser supports the orientationchange DOM event, used to detect orientation changes in the target device.
dojo/on
event-stopimmediatepropagation
Browser supports the stopImmediatePropagation method on DOM events, used to prevent other event listeners being called.
dojo/query
array-extensible
Native array implementation supports manual extension (not supported in older versions of IE).
dojo/ready
dojo-config-addOnLoad
Consume addOnLoad configuration property.
dojo/request/handlers
activex
Browser platform has ActiveX API methods, provided by Internet Explorer
dojo/request/script
script-readystatechange
DOM supports onreadystatechange event, fired when document.readyState changes
dojo/request/xhr
native-xhr
Browser has native XHR API, XMLHttpRequest
dojo/request/xhr
dojo-force-activex-xhr
Force XHR provider to use ActiveX API (MSXMLHTTP).
dojo/request/xhr
native-xhr2
Browser’s native XHR implementation supports XHR Level 2 API
dojo/request/xhr
native-formdata
Browser has a native FormData implementation, letting user compile set of key/value pairs to send using XMLHttpRequest
dojo/selector/_loader
dom-qsa2.1
Browser supports the DOM QuerySelectorAll method available, with Level 2.1 CSS selectors
dojo/selector/_loader
dom-qsa3
Browser supports DOM QuerySelectorAll method, with Level 3 CSS selectors
dojo/selector/lite
dom-matches-selector
Browser supports the matchesSelector method for testing selector queries directly against DOM nodes.dojo/selector/lite, dom-qsa, Browsers supports the DOM QuerySelectorAll method.”
dojo/sniff
air
Environment is running on the Adobe Air platform
dojo/sniff
khtml
Environment is running on the Konqueror-based platform
dojo/sniff
webkit
Environment is running on the WebKit rendering engine platform
dojo/sniff
chrome
Environment is running on the Chrome browser platform
dojo/sniff
safari
Environment is running on the Safari browser platform
dojo/sniff
mac
Environment is running on the Mac OS X platform
dojo/sniff
quirks
Browser is running in Quirks-Mode
dojo/sniff
ios
Environment is running on the iOS mobile operating system
dojo/sniff
android
Environment is running on the Android mobile operating system
dojo/sniff
opera
Environment is running on the Opera browser platform
dojo/sniff
mozilla
Environment is running on the Mozilla browser platform
dojo/sniff
ff
Environment is running on the Firefox browser platform
dojo/sniff
ie
Environment is running on the Microsoft Internet Explorer browser platform
dojo/sniff
wii
Environment is running on the Nintendo Wii browser platform
dijt/BackgroundIframe
config-bgIframe
Flag to create background iframe behind popups like Menus and Dialog. A background iframe prevents problems with popups appearing behind applets/pdf viewers, and also prevents the bleed through select problem on IE6 and IE7.
dijit/_WidgetBase
dijit-legacy-requires
Make dijit load modules the application didn’t explicitly require, e.g. dijit/_base/manager, backwards compatibility in non-async mode.
dijit/form/_ExpandingTextAreaMixin
textarea-needs-help-shrinking
Browser platform’s <textarea> element needs manual help to shrink as content changes.
dojox/form/uploader/Base
FormData
Browser has a native FormData implementation, letting user compile set of key/value pairs to send using XMLHttpRequest
dojox/form/uploader/Base
xhr-sendAsBinary
Browser’s native XHR implementation supports the sendAsBinary method, for sending binary data over XHR.
dojox/form/uploader/Base
file-multiple
Browser supports file input DOM element with multiple file selection attribute, allowing user to select more than one file.
dojox/mobile/Audio
mobile-embed-audio-video-support
Platform supports creating embed tags with audio and video elements.
dojox/mobile/common
mblAndroidWorkaround
Test for Android 2.X transition animation flicker issue
dojox/mobile/common
mblAndroid3Workaround
Test for Android 3.X transition animation flicker issue
dojox/mobile/scrollable
translate3d
Browser supports the WebKit-specific CSS transform property, translate3d.
dojox/mobile/sniff
bb
Environment is running on the RIM Blackberry mobile browser platform
dojox/mobile/sniff
android
Environment is running on the Android mobile browser platform
dojox/mobile/sniff
iphone
Environment is running on the iPhone mobile browser platform
dojox/mobile/sniff
touch
Touch events are supported on the current platform
dojox/mvc/_InlineTemplateMixin
dom-qsa
Browser supports the DOM QuerySelectorAll method
dojox/mvc/parserExtension
dom-qsa
Browser supports the DOM QuerySelectorAll method
dojox/mvc/parserExtension
dojo-parser
Browser has loaded the dojo/parser module
dojox/mvc/parserExtension
dojo-mobile-parser
Browser has loaded the dojox/mobile/parser module
dojox/mvc/sync
mvc-bindings-log-api
Enable debugging messages for MVC module.