如何在没有JavaScript的情况下检测浏览器并显示消息?

4

如果用户在第一次访问时不点击"允许被阻止的内容",我的网站在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 的方法来检测浏览器并显示消息。有人有想法吗?

1
难道你不应该修复内容协议吗?如果它不能正常工作,没有人会进入他们的浏览器设置去做你想要的事情...他们只会离开你的网站。 - charlietfl
IE条件注释可能会有所帮助,或者您可以使用一些特殊的媒体查询,例如:@media all and (-ms-high-contrast: none), (-ms-high-contrast: active)。 - axel.michel
1
@axel.michel IE条件语句在IE9以上的版本不被支持。 - charlietfl
@charlietfl 我知道,但是对于IE9,您可以使用媒体查询和以下条件注释。 - axel.michel
2个回答

3
在网站上为每个用户显示文本,并使用JavaScript隐藏它,以便仅启用了JavaScript的用户可以看到它。使用这种方法,每个用户(包括IE以外的其他浏览器)都可以看到它,因此它应该更加通用。
HTML:
<div id="js-message">You don't have JavaScript enabled, please enable it.</div>

JavaScript(页面加载时):

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

使用<noscript>标签可以使代码更简单。 - charlietfl
创意解决方案!太棒了,因为它将在所有阻止JS的浏览器上显示!谢谢。 - Isaiah Kahler
@charlietfl 使用<noscript>存在一些问题 http://javascript.about.com/od/reference/a/noscriptnomore.htm - Reeno

0
将JavaScript移动到head标签中,在加载ActiveX控件之前调用alert函数:
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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接