阮一峰的IT笔记:首页 -> 分类 -> Javascript 查看所有文章:按分类 | 按月份

浏览器的Feature Testing

Feature testing (sometimes called capability testing) is a powerful technique for coping with incompatibilities. If you want to use a feature or capability that may not be supported by all browsers, include code in your script that tests to see whether that feature is supported. If the desired feature is not supported on the current platform, either do not use it on that platform or provide alternative code that works on all platforms.


For example, there is code that looks like this:

if (element.addEventListener) { // Test for this W3C method before using it
element.addEventListener("keydown", handler, false);
element.addEventListener("keypress", handler, false);
}
else if (element.attachEvent) { // Test for this IE method before using it
element.attachEvent("onkeydown", handler);
element.attachEvent("onkeypress", handler);
}
else { // Otherwise, fall back on a universally supported technique
element.onkeydown = element.onkeypress = handler;
}

« 明年在自动贩卖机(ATM)上买书 | 首页 | Conditional Comments in Internet Explorer »

About

This page contains a single entry from the blog posted on 2006年12月26日 16:18.

The previous post in this blog was 明年在自动贩卖机(ATM)上买书.

The next post in this blog is Conditional Comments in Internet Explorer.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment