【问题标题】:Browser's 'Back' button goes to second to last page. Why?浏览器的“返回”按钮转到倒数第二页。为什么?
【发布时间】:2010-12-06 22:52:29
【问题描述】:

添加这段简短的js代码后:

$(document).ready(function() {
    //check to see if it's an external link and if so do the splashpage
    $('a').click(function() {
        var $this = $(this);

        //get the redirect url
        var redirect_url = $this.attr('href');
        var string_url = String(redirect_url);
        if (string_url.indexOf("http") !== -1) {


            $('#external_link_modal').modal({
                overlayClose: true,
                overlayCss: {
                    backgroundColor: "#ebebeb"
                }
            });


            setTimeout(function() {
                window.location.replace(string_url);
            }, 2500);
            return false;
        }

    });
});

当有人点击一个链接(比如页面 A)时,它会弹出一个警告,然后在一段时间后将用户重定向到新的 url(比如页面 B)。

但是,当有人在页面 B 上单击浏览器的“返回”按钮时,它并没有像预期那样将用户带到页面 A,而是将用户带到之前页面 A 浏览过的页面。

真的很奇怪。 有人知道为什么吗?

先谢谢了!

【问题讨论】:

  • 这里没有 JS 专家,但这是使用索引值与非索引值的问题吗?

标签: javascript browser back-button


【解决方案1】:

window.location.replace(string_url)string_url 替换当前历史条目。您应该改为设置window.location.href

window.location.href = string_url;

【讨论】:

  • 谢谢。这很有效,并欣赏对浏览器“后退”按钮行为的解释。
【解决方案2】:

我还没有测试过,但尝试替换:

window.location.replace(string_url)

与:

window.location.href=string_url

让我知道这是否解决了问题,或者它是否表现出相同的行为。

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多