You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过click()触发重定向时浏览器标签页历史未保留的问题求助

Programmatic anchor click() doesn't preserve browser history (back button is unavailable)

I'm stuck on an issue where triggering a redirect via an anchor tag's click() method programmatically doesn't leave a history entry in the browser tab—and that's messing up my application flow. Let me walk through what's happening:

First, the static version in page1a.html behaves exactly as you'd expect:

<a href="page2.html">Redirect to Page 2</a>

If a user opens page1a.html in a new tab and clicks the link, they land on page2.html, and the browser's back button works normally—clicking it takes them right back to page1a.html.

Dynamic Click (History Missing)

But when I automate the click event in page1b.html, things break:

<a id="redirect" href="page2.html"></a>
<script>
document.onload = function () {
 document.getElementById("redirect").click();
};
</script>

Opening page1b.html does redirect to page2.html, but the back button is grayed out—there's no trace of page1b.html in the tab's history. It's like the page never even loaded.

I get that there might be a security or usability reason browsers do this, but it's causing problems for my app. page2.html has tons of dynamic interactions, and users frequently hit the back button. My app is supposed to capture that back navigation intent from the original page1 context to handle state cleanup and other actions.

I've searched everywhere and haven't found any info on this behavior. At first I thought it was a Chrome bug, but I tested in Opera and got the same result.

Could someone explain why this happens? And if this is intentional browser behavior, what's a solid workaround to make sure the history is preserved so the back button works like it does with a manual click?

内容的提问来源于stack exchange,提问作者MartinJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:02:44