条件注释¶
ID: js/conditional-comment
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- portability
- maintainability
- language-features
- external/cwe/cwe-758
Query suites:
- javascript-security-and-quality.qls
条件注释仅在 Internet Explorer 中受支持,应避免使用以确保可移植性。
建议¶
使用特征检测(如 jQuery 等主要框架提供的)代替。
示例¶
以下代码段使用条件注释来检测它是否在 Internet Explorer 9 或更高版本上运行。更好的方法是使用例如 jQuery 的 $.support
对象直接检查所需的功能。
/*@cc_on
@if (@_jscript_version >= 6)
console.log("You're running a new version of IE.");
@else
console.log("You're running an old version of IE.");
@end
@*/
请注意,条件注释在 Internet Explorer 11 标准模式中不再受支持。
参考资料¶
Internet Explorer 开发中心:@cc_on 语句(JavaScript).
通用弱点枚举:CWE-758.