我通过单击具有特定类的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
});
当前回答
Necromancing。
为了不依赖于user-agent字符串,只需检查以下几个属性:
if (document.documentMode)
{
console.log('Hello Microsoft IE User!');
}
if (!document.documentMode && window.msWriteProfilerMark) {
console.log('Hello Microsoft Edge User!');
}
if (document.documentMode || window.msWriteProfilerMark)
{
console.log('Hello Microsoft User!');
}
if (window.msWriteProfilerMark)
{
console.log('Hello Microsoft User in fewer characters!');
}
此外,这将检测新的Chredge/Edgium(阿纳海姆):
function isEdg()
{
for (var i = 0, u="Microsoft", l =u.length; i < navigator.plugins.length; i++)
{
if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
return true;
}
return false;
}
这个可以检测铬:
function isChromium()
{
for (var i = 0, u="Chromium", l =u.length; i < navigator.plugins.length; i++)
{
if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
return true;
}
return false;
}
还有这个Safari:
if(window.safari)
{
console.log("Safari, yeah!");
}
其他回答
你可以使用$。浏览器获取名称、供应商和版本信息。
参见http://api.jquery.com/jQuery.browser/
我用过这个
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;
}
}
如果你只想知道浏览器是否是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
}
您可以检测到所有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/
JavaScript检测ie或Edge版本的功能
function ieVersion(uaString) {
uaString = uaString || navigator.userAgent;
var match = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(uaString);
if (match) return parseInt(match[2])
}