【问题标题】:How to enable history back button when using location.href = "site"?使用 location.href = "site" 时如何启用历史记录返回按钮?
【发布时间】:2021-04-03 01:06:55
【问题描述】:

我是 js 和 html 的新手。我有一个 js 代码可以重定向到同一站点中的网页,从 https://example.com/html1.html 到 https://example.com/html2.html。我也想要它,以便可以启用后退按钮,但从我通过搜索发现的内容来看,没有提供我正在寻找的答案。

代码如下:

location.href = "https://example.com/html2.html";

由于某种原因,返回上一页的后退按钮 (https://example.com/html1.html) 不起作用。

location.href = "https://stackoverflow.com/";
//Firebase authstate changed listener here
//Timeout represents when the user signs in
setTimeout(() => {location.href = "https://example.com/";}, 500);

我有一个热修复程序,它并不总是有效,但它只是延迟 1 秒,它可以工作

firebase.auth().onAuthStateChanged(user => {
    if (user) {
        console.log("User: " + user.email + " Displayname: " + user.displayName);
        console.log("Url: " + location.href);
        console.log(location.href == "site1"); //Logging if we are in the site1 or not
        if (location.href == "site1") {
            setTimeout(() => {
                location.href = "site2";
            }, 1000);
            console.log("Not in site2");
        } else {
            console.log("In site2");
        }
        console.log("Going to main blog");
    }

});

【问题讨论】:

  • 没有理由它不应该工作。在此页面上打开您的控制台并运行 location.href = 'https://google.com' 并从那里尝试返回按钮。应该带你回到这里
  • @charlietfl 哇,这真的很奇怪。由于某种原因,它在我的网页上不起作用......我会检查一下。它只在我的网站上工作过一次......在我再次尝试后它就停止了。编辑:现在它再次工作。似乎不一致或只是因为我使用的是 Firebase 网络托管?
  • 当两个 url 都正确解析时,无法看到主机将如何发挥作用。浏览器存储历史。可能与浏览器扩展有关。尝试禁用它们或查看是否可以在隐身(私人)模式下复制
  • 另一件事。您没有在页面加载时使用该权限吗?
  • @charlietfl 不,我在 firebase.auth().onAuthStateChanged 中使用它,但由于 authstate 几乎与页面加载一样即时,这有关系吗?

标签: javascript html browser-history


【解决方案1】:

试试 window.location('link') 应该工作

【讨论】:

    【解决方案2】:

    试试:

    window.location.href = "URL"
    

    这应该模拟一个按钮按下,这意味着它应该允许你返回一个页面。

    【讨论】:

      猜你喜欢
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2023-03-31
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多