How to prevent page loading

Asked

Viewed 950 times

-1

There is an ERP system in the company where I maintain, and constantly end up supporting user errors.

It turns out that my biggest problem I have, is when the user does not complete the entire action that it stopped and page exchange, causing timeout on the previous page and consequently giving me more work.

My doubt is simple (I believe), I need to create a type of alert or confirm, that in case the user requests the loading of another page (through the menu) or close the tab/browser, on the condition that I have not pressed the "X" button he receives this warning, to force him to press the "X" button and only after being able to leave the page where he is.

  • 2

    Answer your question: http://answall.com/questions/40982/display-algo-ao-tenta-close-meu-site

  • 1

    André, I left an answer with examples of me. It would be better if you put your HTML in the question for the answer(s) to be closer to your case.

1 answer

0

For that you’ll need a flag, something that keeps the state "clicked"/"not yet clicked". You can do this with a variable or maybe even better in the element itself that should be clicked.

For example, this element/button that has the "X":

<button id="verificador" data-x="false" type="button">X</buton>

And then in the code that allows page change you have to go check the status of that data-field. For example:

$('#menu a').on('click', function(){
   var estado = $('#verificador').data('x'); 
    if (!estado){
       alert('Por favor carregue no botão!');
       return false;
    }

    // continuar caso o estado seja true
});

On the button itself, to change the state when clicked you must also have:

$('#verificador').on('click', function(){
   $(this).data('x', true); 
});

or only $(this).data('x', true); if there is already an event receiver for that helmet somewhere in your code.

Another alternative is to open an Alert asking if you’re sure you want to refresh the page like described in this other answer.

Browser other questions tagged

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