简体   繁体   English

onunload 事件在 window.close 之前触发

[英]onunload event fires before window.close

I am trying to add Spotify Login pop-up to my website.我正在尝试将 Spotify 登录弹出窗口添加到我的网站。 Everything is ready but window.unonload runs before i close window.一切准备就绪,但 window.unonload 在我关闭窗口之前运行。

This is really going to drive me crazy.这真的会让我发疯。 I'm trying to find solution for 2 hours in web but couldn't.我试图在网上找到 2 小时的解决方案,但没有找到。

Look at this image.看看这张图片。 Window is still open but unload event fired窗口仍然打开但卸载事件被触发在此处输入图片说明

/**
 * @description Spotify Login. Creates New Window.
 */
export const authorize = async () => {

    let url = await axios.get(apiUrl("/api/authorize"));

    var loginWindow = window.open(url.data, "Spotify Login", "width=600,height=800");

    loginWindow.onunload = () => {
        console.log("Fetching Token load");
    }
}

I found a solution.我找到了解决方案。 In new window when i get token i save it to localstorage and close the window.在新窗口中,当我获得令牌时,我将其保存到 localstorage 并关闭窗口。 In main window i watch for changes in localstorage then do my stuff and remove the event.在主窗口中,我观察 localstorage 的变化,然后做我的事情并删除事件。

export const authorize = async () => {
    let url = await axios.get(apiUrl("/api/authorize"));
    window.open(url.data);
    const handleStorage = () => {
        store.commit('setLogged', true);
        store.commit('setStatus');
        window.removeEventListener('storage', handleStorage)
    }
    window.addEventListener('storage', handleStorage);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM