首页 > 解决方案 > 如何在每次访问网站时显示一次消息?

问题描述

我的客户希望在用户使用 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");
    }
}

标签: javascriptcookies

解决方案


一种可能的解决方案是使用sessionStorageIE8+ 支持的 .

// 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
}

推荐阅读