Preventing page load

-1

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

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

My question is simple (I believe), I need to create a type of alert or confirm , which in case the user requests to load another page (through the menu) or close the browser / If you do not press the "X" button, it will prompt you to press the "X" button and only then you can exit the page it is in.

    
asked by anonymous 26.11.2014 / 12:17

1 answer

0

For this you will need a flag , something that saves the state "clicked" / "not yet clicked". You can do this with a variable or perhaps even better in the element that must 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 check the status of this 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
});

In 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 listener for this elment somewhere in your code.

Another alternative is to open an alert asking if you are sure you want to refresh the page as I have described in this other response .

    
26.11.2014 / 12:24