如何检测用户是否正在使用 IE 并通知他们该网站不支持 IE

How to detect if a user is using IE and inform them that the site doesn't support IE?

本文关键字:IE 通知 他们 不支持 网站 何检测 检测 是否 用户      更新时间:2023-09-26

我正在使用Zepto创建一个网站,因此它不支持任何Internet Explorer版本。

如何检测用户是否正在使用 Internet Explorer,并将他们重定向到通知该网站不支持 IE 的页面?

我读过有关条件注释的文章,但Internet Explorer 10不支持它们。

谢谢。

你永远无法确定,但一个简单的选择是查看用户代理(例如.PHP)。

另请查看此处: 如何显示浏览器特定的 HTML?

像这样设置文档:

<!doctype html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>

对于 IE10,请使用以下命令:

    if (Function('/*@cc_on return document.documentMode===10@*/')()
){    
document.documentElement.className+=' ie10'; 
}

然后,你对这些类做什么取决于你是想用css显示/隐藏一个div,还是使用javascript来重定向页面。

Javascript:

(function ($) {
    "use strict";
    // Detecting IE
    var IE;
    if ($('html').is('.ie6, .ie7, .ie8, .ie9, .ie10')) {
        IE = true;
    }
    if (IE) {
        // redirect
       window.location.replace('http://www.myotherpage.com');
    } 
}(jQuery));

或 CSS:

.ie6 .myDivClassName,
.ie7 .myDivClassName,
.ie8 .myDivClassName,
.ie9 .myDivClassName,
.ie10 .myDivClassName {
  display: block; 
}

取决于你想做什么。