我通过单击具有特定类的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
});
当前回答
你可以简单地这样做:
var isIE = window.document.documentMode ? true : false; // this variable will hold if the current browser is IE
我知道这个问题很老了,但如果有人滚动那么远,他们可以看到简单的答案:)
其他回答
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) {
使用上面的答案;简单压缩返回布尔值:
var isIE = /(MSIE|三叉戟\/|Edge\/)/i.test(navigator.userAgent);
您可以检测到所有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/
如果你只想知道浏览器是否是IE,你可以这样做:
var isIE = false;
var ua = window.navigator.userAgent;
var old_ie = ua.indexOf('MSIE ');
var new_ie = ua.indexOf('Trident/');
if ((old_ie > -1) || (new_ie > -1)) {
isIE = true;
}
if ( isIE ) {
//IE specific code goes here
}
更新1:一个更好的方法
我现在就推荐这个。它仍然是非常可读的,并且代码更少:)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
感谢JohnnyFun在评论中给出的简短答案:)
更新2:在CSS中测试IE
首先,如果可以的话,你应该使用@supports语句而不是JS来检查浏览器是否支持某个CSS特性。
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
(注意IE根本不支持@supports,并且会忽略任何放在@supports语句中的样式。)
如果这个问题不能通过@supports解决,那么你可以这样做:
// JS
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
document.documentElement.classList.add('ie')
}
/* CSS */
.element {
/* styles that apply everywhere */
}
.ie .element {
/* styles that only apply in IE */
}
(注意:classList对于JS来说相对较新,我认为,在IE浏览器之外,它只能在IE11中工作。也可能是IE10。)
如果你在项目中使用SCSS (Sass),这可以简化为:
/* SCSS (Sass) */
.element {
/* styles that apply everywhere */
.ie & {
/* styles that only apply in IE */
}
}
更新3:添加Microsoft Edge(不推荐)
如果您还想将Microsoft Edge添加到列表中,您可以执行以下操作。但是我不推荐它,因为Edge是一个比IE强大得多的浏览器。
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);
if ( isIE ) {
//IE & Edge specific code goes here
}
我想这会对你有帮助
function checkIsIE() {
var isIE = false;
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
isIE = true;
}
if (isIE) // If Internet Explorer, return version number
{
kendo.ui.Window.fn._keydown = function (originalFn) {
var KEY_ESC = 27;
return function (e) {
if (e.which !== KEY_ESC) {
originalFn.call(this, e);
}
};
}(kendo.ui.Window.fn._keydown);
var windowBrowser = $("#windowBrowser").kendoWindow({
modal: true,
id: 'dialogBrowser',
visible: false,
width: "40%",
title: "Thông báo",
scrollable: false,
resizable: false,
deactivate: false,
position: {
top: 100,
left: '30%'
}
}).data('kendoWindow');
var html = '<br /><div style="width:100%;text-align:center"><p style="color:red;font-weight:bold">Please use the browser below to use the tool</p>';
html += '<img src="/Scripts/IPTVClearFeePackage_Box/Images/firefox.png"/>';
html += ' <img src="/Scripts/IPTVClearFeePackage_Box/Images/chrome.png" />';
html += ' <img src="/Scripts/IPTVClearFeePackage_Box/Images/opera.png" />';
html += '<hr /><form><input type="button" class="btn btn-danger" value="Đóng trình duyệt" onclick="window.close()"></form><div>';
windowBrowser.content(html);
windowBrowser.open();
$("#windowBrowser").parent().find(".k-window-titlebar").remove();
}
else // If another browser, return 0
{
return false;
}
}