簡體   English   中英

為什么單擊后退按鈕時 popstate 不會觸發我的函數? (手機 ios 鉻)

[英]Why popstate doesnt fire my function when back button is clicked? (mobile ios chrome)

我正在嘗試在新選項卡中打開鏈接時這樣做,可以通過后退按鈕返回引薦來源頁面。

const referrer = document.referrer;
const redirect = (e) => {
     if(e.state.goBack){
        window.location.href = window.location.href;
     }
 }

const _location = window.location.href
history.replaceState({goBack: true}, null, referrer);
history.pushState({}, null, _location);

window.addEventListener('popstate', redirect);

在 MacBook、android chrome/firefox 中工作正常。 僅在 iPhone/iPad 中,它不起作用。 它只會回到自己身上。 即使我在重定向功能中添加警報,當我單擊 iPad chrome 中的后退按鈕時,它也不會顯示。 :(

感覺像 iPad 上的后退按鈕點擊不會觸發重定向功能。

我遇到了同樣的問題。 我發現的解決方法不是使用 replaceState 而是使用 pushState: https ://jsfiddle.net/6dayLhzs/1

const referrer = document.referrer;
const redirect = (e) => {
    if(e.state.goBack){
        window.location.href = window.location.href;
    }
 }

const _location = window.location.href
history.pushState({goBack: true}, null, referrer);
history.pushState({}, null, _location);

window.addEventListener('popstate', redirect);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM