【问题标题】:to show an alert once user backs to a page用户返回页面后显示警报
【发布时间】:2020-04-09 14:18:12
【问题描述】:

有两页,当用户从第二页回到第一页时,我想在第一页显示警报。

我知道如何返回首页:window.location.href

还有:window.history.back()

但我不知道一旦它被支持后如何显示警报?

【问题讨论】:

标签: javascript html window


【解决方案1】:

您可以使用localStoragesessionStorage 轻松实现。

//第1页

const visited = localStorage.getItem("visited")

if(visited) alert("//Do something here, why you came back")

else localStorage.setItem("visited", "visited")

【讨论】:

    【解决方案2】:

    您可以给 url 一个参数并使用它来检查是否应该触发警报消息。

    例如:window.location.search -> '?visited=true'

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 2020-11-18
      • 2020-10-08
      • 1970-01-01
      相关资源
      最近更新 更多