简体   繁体   中英

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:

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

After the Ajax call following code makes sure the loading panel disappears again:

    $(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 . In this case I want the ajaxComplete function NOT to hide the loading panel too early until the redirect has been succeeded.

How can I check in the ajaxComplete function if the window.location.href has been changed and the page is about to redirect?

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;
2. In your jax call Onsuccess function set isHide = false if hiding is not require.
3. in your ajaxComplete see following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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