Intercept page redirection

Asked

Viewed 187 times

3

I have a page whose customer is selecting items, after selected I needed to check if the customer will leave the current page because I need to alert him with a message like "Do you want to save your cart before leaving the page?" if saved and continues redirect, if not just redirect. I wanted to do for global Redirects because I could make appear a pop-up asking if he wanted to and then do a window.location.href but I want it to fit for everyone and not do one by one the Redirects... I got a javascript method that does an action before redirecting, but even showing a pop-up it redirects without expecting customer response. I want to know if there’s any way I can intercept the redirect and after his response I’ll let him redirect

The function js

 $(window).on("beforeunload", function(){
    console.log("Deixando a pagina");
    $('#modal-form-call').iziModal('open');
});

but there in the form it does not wait for the answer and already redirects

1 answer

1

Browsers tend to block this sort of thing more and more.

If you try to use a function confirm at the event beforeUnload, to be more assertive for example, Chrome blocks the function call. I believe other browsers do the same.

If I’m not mistaken, I’ve seen people ask questions about how to circumvent that kind of behavior, for extensions or things like that. I wouldn’t be surprised if any ad Blocker of life start messing with it.

For these reasons, I believe that interrupting the most natural flow of user actions is not a good way. I would continue this research if-only-if this is a requirement for reasons of law - I know that in Europe there are laws related to how you can communicate with your users on your page.

But if this is just one Feature, my suggestion is to always save the cart in the browser’s local storage (and possibly on the server as well) every change in the cart. This gives a much more enjoyable user experience and you don’t worry about interface streams and substreams, nor about trying to convince the browser and its extensions about your code not being malicious.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.