这个问题已经在这里有一个答案:
- 我怎么只针对Internet Explorer的10,如Internet Explorer特定CSS或Internet Explorer的特定的JavaScript代码的某些情况呢? 25个回答
我怎样才能让一个消息框出现在页面加载,如果用户使用的是IE 10?
function ieMessage() {
alert("Hello you are using I.E.10");
}
我的网页是一个JSF的facelet(XHTML)。
这个问题已经在这里有一个答案:
我怎样才能让一个消息框出现在页面加载,如果用户使用的是IE 10?
function ieMessage() {
alert("Hello you are using I.E.10");
}
我的网页是一个JSF的facelet(XHTML)。
检测到这一点,没有条件注释,没有用户代理嗅探真正的方法是使用条件编译:
<script type="text/javascript">
var isIE10 = false;
/*@cc_on
if (/^10/.test(@_jscript_version)) {
isIE10 = true;
}
@*/
console.log(isIE10);
</script>
运行此代码后,您可以使用后随时以下几点:
if (isIE10) {
// Using Internet Explorer 10
}
参考: 如何从JS检测IE10浏览器时,模式是IE9?
更新:
为了避免意见微小,你可以使用这样的:
var IE = (function () {
"use strict";
var ret, isTheBrowser,
actualVersion,
jscriptMap, jscriptVersion;
isTheBrowser = false;
jscriptMap = {
"5.5": "5.5",
"5.6": "6",
"5.7": "7",
"5.8": "8",
"9": "9",
"10": "10"
};
jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();
if (jscriptVersion !== undefined) {
isTheBrowser = true;
actualVersion = jscriptMap[jscriptVersion];
}
ret = {
isTheBrowser: isTheBrowser,
actualVersion: actualVersion
};
return ret;
}());
和访问等的属性IE.isTheBrowser
和IE.actualVersion
(从JScript的版本的内部值翻译)。
在一般情况下,用户代理的做法嗅探和条件编译/注释最好避免。 这是迄今为止最好使用特征检测 , 优雅降级和渐进增强来代替。 然而,对于一些边缘情况下,更方便开发人员来检测浏览器的版本,可以使用下面的代码片段:
这if
语句只会执行在IE 10
// IF THE BROWSER IS INTERNET EXPLORER 10
if (navigator.appVersion.indexOf("MSIE 10") !== -1)
{
window.alert('This is IE 10');
}
这if
语句将只执行的IE浏览器11
// IF THE BROWSER IS INTERNET EXPLORER 11
var UAString = navigator.userAgent;
if (UAString.indexOf("Trident") !== -1 && UAString.indexOf("rv:11") !== -1)
{
window.alert('This is IE 11');
}
http://jsfiddle.net/Qz97n/
以下是获取当前IE或IE版本的方法:
function IE(v) {
return RegExp('msie' + (!isNaN(v)?('\\s'+v):''), 'i').test(navigator.userAgent);
}
这里是你如何使用它:
if(IE()) alert('Internet Explorer!');
if(IE(10)) alert('Internet Explorer 10!');