【发布时间】:2019-04-24 09:56:33
【问题描述】:
您知道仅适用于 Internet Explorer 用户的通知或警告脚本吗?
我只需要在此特定浏览器中为用户显示警告。
拜托,你能帮帮我吗?
【问题讨论】:
标签: javascript
您知道仅适用于 Internet Explorer 用户的通知或警告脚本吗?
我只需要在此特定浏览器中为用户显示警告。
拜托,你能帮帮我吗?
【问题讨论】:
标签: javascript
前段时间我不得不解决这个问题。由于放弃了对条件 cmets 的支持,我最终使用了 javascript:https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/hh801214(v=vs.85)
我的解决方案最终看起来像这样:
<style>
#ie-banner {
display: none;
/* other styling */
}
</style>
<div id="ie-banner">
<div id="ie-message">
<h5>INCOMPATIBLE BROWSER</h5>
</div>
</div>
<script>
function isOldIE(userAgent) {
var msie = userAgent.indexOf('MSIE');
if (msie > 0) {
// IE 10 or older
return true;
}
// other browser, IE 11, or Edge
return false;
}
if (isOldIE(navigator.userAgent)) {
var ieWarning = document.getElementById('ie-banner');
ieWarning.setAttribute('style', 'display: block;');
// drop off my react app below
// var root = document.getElementById('root');
// document.body.removeChild(root);
}
</script>
请注意,我删除了这样的子元素并使用较旧的 DOM api,因为更多标准方法根本无法在 IE 上运行...大惊喜。
如果您只关心 IE9 及以下版本,那么我可能只会使用条件 cmets。直接来自上面的链接:
<html>
<!--[if IE]>
This content is ignored in IE10 and other browsers.
In older versions of IE it renders as part of the page.
<![endif]-->
</html>
【讨论】: