将“Home”哈希插入浏览器历史记录中

时间:2012-11-27 18:37:28

标签: javascript webpage browser-history

我想实现以下目标:

如果用户打开了指向我网站中某个子页面的链接,例如index.html #Contact,我想在index.html #Contact标记之前将index.html#token添加到历史堆栈中,所以当用户点击浏览器的返回按钮时,首先在我的网站的起始页面上结束,然后在下一个后面实际上使页面正常离开。< / p>

我认为我可以通过以下执行“onload”执行的javascript轻松实现此目的:

var href = window.location.href;
var hashPos = href.indexOf("#");
if ((hashPos > 0) & (hashPos+1 < href.length)) {
    window.location.replace(href.substring(0, hashPos+1));
    window.location.assign (href);
}

但由于某种原因,这根本不起作用......

我让它在Firefox中工作:

var href = window.location.href;
var hashPos = href.indexOf("#");
if ((hashPos > 0) & (hashPos+1 < href.length)) {
    window.location.replace(href.substring(0, hashPos+1));
    setTimeout(function() { 
        window.location.assign(href);
    }, 0);
}

但是在IE和Chrome中,这也没有做任何事情......

任何建议(除了“不要这样做!”)?

0 个答案:

没有答案