【问题标题】:How to detect browser and show message without javascript?如何在没有 javascript 的情况下检测浏览器并显示消息?
【发布时间】:2015-10-11 04:18:56
【问题描述】:

好的,除非用户在第一次访问时单击“允许被阻止的内容”,否则我的网站无法在 Internet Explorer 中正常运行。现在我想出了一些 javascript 来检测打开网页的浏览器并弹出一个显示:'你在 Internet Explorer 中。请在重新加载时单击页面底部的“允许阻止的内容”以允许运行脚本和 ActiveX 控件。否则网站可能无法正常工作。但它本身就是一个脚本,所以用户只有在推送它时才能看到它。现在它没用,因为它告诉用户只有在他们允许之后才能点击按钮。 我的 JS 是:

var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isFirefox = typeof InstallTrigger !== 'undefined'; 
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
var isChrome = !!window.chrome && !isOpera;              
var isIE = /*@cc_on!@*/false || !!document.documentMode; 
if(isChrome === false && isSafari === false && isFirefox === false && isOpera === false)
    {alert('You are in Internet Explorer. Please allow running scripts and ActiveX controls by clicking "Allow Blocked Content" at the bottom of the page upon reload. Otherwise the site may not work correctly.'
);};

我需要一种方法来检测浏览器并在没有任何 JS 或 JQuery 的情况下显示消息。有人有想法吗?

【问题讨论】:

  • 您不应该修复内容协议吗?没有人会进入他们的浏览器设置来做你想做的事......如果它不起作用,他们就会离开你的网站
  • IE 条件 cmets 可能会有所帮助,或者您可以使用一些特殊的媒体查询,例如:@media all and (-ms-high-contrast: none), (-ms-high-contrast: active)
  • @axel.michel IE9 以上不支持 IE 条件
  • @charlietfl 我知道,但是对于 IE9,您可以使用媒体查询和低于条件 cmets。

标签: javascript jquery internet-explorer


【解决方案1】:

在网站上为每个用户显示一个文本并用 JavaScript 隐藏它,因此只有没有启用 JavaScript 的用户仍然可以看到它。使用这种方法,每个用户(也包括 IE 以外的其他浏览器)都能看到它,因此它应该更通用。

HTML:

<div id="js-message">You don't have JavaScript enabled, please enable it.</div>

JavaScript(页面加载):

$(function() {
     $('#js-message').remove();
});

【讨论】:

【解决方案2】:

将 javascript 移动到上面加载 ActiveX 控件并在那里调用警报函数的 head 标记中:

if (window.navigator.userAgent.indexOf("MSIE ") > -1) {
    alert('You are in Internet Explorer...');
}

如果这不起作用,请将消息以粗体放在页面顶部的 div 中并将其隐藏:

<div id="ienotice">You are in Internet Explorer...</div>

setTimeout(function() {
    document.getElementById('ienotice').style.display = 'none';
}, (window.navigator.userAgent.indexOf("MSIE ") > -1) ? 5000 : 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多