javascript - 如何在每次访问网站时显示一次消息?
问题描述
我的客户希望在用户使用 IE 11 时显示横幅,但他们只希望它显示在用户输入的页面上。如果他们导航到其他页面,它应该会消失。而且,如果他们离开网站并回来,它应该再次显示横幅。我已经弄清楚了检测 IE 11,但我真的不知道从哪里开始让另一部分工作。有什么帮助吗?
这是我到目前为止所拥有的:
function isIE(){
return window.navigator.userAgent.match(/(MSIE|Trident)/);
}
//function to show alert if it's IE
function ShowIEAlert(){
if(isIE()){
document.getElementById('ie11-banner').classList.remove("hidden");
}
}
解决方案
一种可能的解决方案是使用sessionStorage
IE8+ 支持的 .
// example of feature detection for an old browser like IE11
const isIE11 = "documentMode" in document && !("includes" in String.prototype);
// the key that you store upon first entering the site
const firstVisit = sessionStorage.getItem("firstVisit") !== null;
if ( firstVisit === true ) {
// this should be their second+ time visiting; do nothing
} else {
sessionStorage.setItem("firstVisit", true);
// can be any value, even `undefined`, just not `null`, as that is the default value for Storage values
// this should their first time visiting
}
推荐阅读
- firebase - 如何通过 gcloud 将 tflite 文件上传到 firebase 测试实验室设备?
- scala - 当 NullType 在 StructType 中时,将空值写入 Spark 中的 Parquet
- java - pdfbox:java.lang.NumberFormatException:对于输入字符串:“1#1”
- apache-spark - Amazon EMR 未分配所有请求的内存
- ffmpeg - 将媒体文件转换为 mp4 时 ffmpeg -i 不起作用
- c# - 如何使用 C# 在系统托盘中显示进度面板?
- java - 不能从静态上下文中引用
- ios - 如何在Swift中的动态角度开关上使线条弯曲?
- selenium-chromedriver - Selenium 属性错误“'WebDriver' 对象没有属性 '_timeout'”
- java - 在同一个 hybris 实例中具有多个 basestore 和 basesite 的 Hybris-Common 代码方法