简体   繁体   English

JavaScript:检查window.location.href是否已更改

[英]JavaScript: Check if window.location.href has been changed

I have following code to show a loading panel after eg clicking a button and making an Ajax call: 我有以下代码来显示加载面板,例如,单击按钮并进行Ajax调用后:

    $(document).on("click", function (e) {
        window.LoadingPanel.Show();
    });

After the Ajax call following code makes sure the loading panel disappears again: 在Ajax调用之后,以下代码确保加载面板再次消失:

    $(document).ajaxComplete(function() {
        if (window.LoadingPanel != null) {
            window.LoadingPanel.Hide();
        }
    });

But in some cases I redirect to another page depending on the result of the Ajax call by setting the window.location.href . 但是在某些情况下,我会通过设置window.location.href来根据Ajax调用的结果重定向到另一个页面。 In this case I want the ajaxComplete function NOT to hide the loading panel too early until the redirect has been succeeded. 在这种情况下,我希望ajaxComplete函数不要ajaxComplete隐藏加载面板,直到重定向成功为止。

How can I check in the ajaxComplete function if the window.location.href has been changed and the page is about to redirect? 如果window.location.href已更改并且页面将要重定向,如何检查ajaxComplete函数?

This might not be a good way to solve this problem but thats what i can think of now. 这可能不是解决此问题的好方法,但这就是我现在能想到的。
1. create a variable call isHide = true; 1.创建一个变量,调用isHide = true;
2. In your jax call Onsuccess function set isHide = false if hiding is not require. 2.在jax调用中,如果不需要隐藏,则将Onsuccess函数设置为isHide = false。
3. in your ajaxComplete see following 3.在您的ajaxComplete中,请参阅以下内容

$(document).ajaxComplete(function(e) {
    if (window.LoadingPanel != null && isHide) {
        window.LoadingPanel.Hide();
    }
});

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

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