JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

13.4.3 Feature Testing

13.4.3 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.

You’ll see feature testing again and again in the chapters that follow. In Chapter 17 , for example, we use 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;


友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1