Neeraj N
Neeraj N

Reputation: 53

How to remove the alert of "Leave Site" in javascript?

I am having a ts file in which I have written a function to logout which is :

private addListenersForLogout() {
        if (this._logoutDiv) {
            this._logoutDiv.addEventListener("click", (e) => {
                DataManager.getDataManager().geHttpDataHandler().postRequest("", HTMLFactory.POST_USER_LOGOUT, () => {
                    window.location.href = Constants.LOGIN;
                }, () => {
                    Console.log("Failed");
                })
            })
        }
    }

Here , this._logoutDiv is a div element on which I am adding click listener . When the user clicks it one backend API is called and based upon the response I am redirecting to the login page . But as soon as I am clicking it and after I get the response from the backend it shows an alert box of Leave Site . How can I remove that ?? or Is there any alternative to window.location.href to redirect ??

Here is the screenshot of the alert message . Leave Site alert

Thanks

Upvotes: 2

Views: 2251

Answers (1)

Porcellus
Porcellus

Reputation: 579

This dialog doesn't come from the code you posted here: something is hooking into the onbeforeunload event somewhere else on the site.

You either need to skip adding it or you could remove it just before the navigation. To do that, you need to call window.removeEventListener with the same function (and event type) that the addEventListener was called with.

As a workaround you could try adding this right before the redirect:

window.onbeforeunload = null;

Upvotes: 3

Related Questions