【问题标题】:How to show a message once per website visit?如何在每次访问网站时显示一次消息?
【发布时间】:2020-11-10 08:18:52
【问题描述】:

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

【问题讨论】:

  • 使用每个会话过期的 cookie
  • navigator.userAgent 正则表达式是您可以尝试确定客户端浏览器是否为 IE11 的最弱方法。
  • 我个人会使用window.localStorage; -- 因为它只是存储“站点偏好”.. cookie 似乎没有必要。
  • 现在,我必须要问了,因为 IE11 是一个死浏览器,就像 Amaya 和 Netscape Navigator 一样,是什么让你这样做呢?
  • @xxh 那么检查浏览器是否为 IE11 的更强大的方法是什么?另外,我这样做只是因为客户想要它。

标签: javascript cookies


【解决方案1】:

一种可能的解决方案是使用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
}

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    相关资源
    最近更新 更多