我通过单击具有特定类的divs调用如下函数。
是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。
如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
我通过单击具有特定类的divs调用如下函数。
是否有一种方法,我可以检查启动函数时,如果用户正在使用Internet Explorer和中止/取消它,如果他们正在使用其他浏览器,以便它只运行于IE用户?这里的用户都使用IE8或更高版本,所以我不需要涵盖IE7和更低版本。
如果我能告诉他们使用的浏览器,这将是伟大的,但不是必需的。
示例函数:
$('.myClass').on('click', function(event)
{
// my function
});
当前回答
Angularjs团队是这样做的(v 1.6.5):
var msie, // holds major version number for IE, or NaN if UA is not IE.
// Support: IE 9-11 only
/**
* documentMode is an IE-only property
* http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
*/
msie = window.document.documentMode;
然后有几行代码分散在使用它作为一个数字,如
if (event === 'input' && msie <= 11) return false;
and
if (enabled && msie < 8) {
其他回答
使用下面的JavaScript方法:
function msieversion()
{
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0) // If Internet Explorer, return version number
{
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
else // If another browser, return 0
{
alert('otherbrowser');
}
return false;
}
您可以在以下微软支持网站上找到详细信息:
如何从脚本确定浏览器版本
更新:(支持IE 11)
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
else // If another browser, return 0
{
alert('otherbrowser');
}
return false;
}
这是另一种不用查看User-Agent就能检测IE的方法:
var usingIE="__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR"在文档中; alert("You are"+(use ingie ?"":"n't")+" using ie .");
我在测试我的网站是否能在IE上运行时偶然发现了这个,我打开调试器,点击文件夹图标。里面有我的脚本,还有一个我没有的动态脚本文件夹。我打开它,发现有很多browsertools.library.js文件。在它们里面,我发现了这样的东西:
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = undefined;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR = false;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERRORCODE = undefined;
try{
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = eval("\r\n//# sourceURL=browsertools://browsertools.library.js");
}
catch( eObj ){
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERRORCODE = eObj.number;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = eObj.message || eObj.description || eObj.toString();
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR = true;
}
所以我用这些来测试用户的浏览器是否是IE。请注意,这只适用于你想知道他们是否有IE,而不是确切地知道他们有什么版本的IE。
您可以检测到所有Internet Explorer(最近测试的版本12)。
<script>
var $userAgent = '';
if(/MSIE/i['test'](navigator['userAgent'])==true||/rv/i['test'](navigator['userAgent'])==true||/Edge/i['test'](navigator['userAgent'])==true){
$userAgent='ie';
} else {
$userAgent='other';
}
alert($userAgent);
</script>
请看这里https://jsfiddle.net/v7npeLwe/
@SpiderCode的解决方案不能在ie11中工作。以下是我在代码中使用的最佳解决方案,我需要对特定功能进行浏览器检测。
IE11不再报告为MSIE,根据这个更改列表,这是为了避免错误检测。
如果你真的想知道它是IE,你可以做的是在用户代理if导航器中检测Trident/字符串。appName返回Netscape,类似于(未测试的);
感谢这个答案
function isIE()
{
var rv = -1;
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
else if (navigator.appName == 'Netscape')
{
var ua = navigator.userAgent;
var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv == -1 ? false: true;
}
我用过这个
function notIE(){
var ua = window.navigator.userAgent;
if (ua.indexOf('Edge/') > 0 ||
ua.indexOf('Trident/') > 0 ||
ua.indexOf('MSIE ') > 0){
return false;
}else{
return true;
}
}