Is it possible to "hold" an event for subsequent shelling?

What I'm trying to do is: when the user clicks the cancel button or moves from the page by a link or menu option, I check if there are unsaved changes. If so, I ask the user if he / she wants to save. I cannot do this using the javascript confirmation window, because sometimes I have more than two parameters, so I cannot “hold” everything until the user makes a choice, for example confirmation. Therefore, I, however, in order to “save” the event, cancel its current execution, wait for the user to use his mind, and then take the necessary actions in accordance with their response, and then raise the original event. So, as an example code of what I thought: I have this piece of code:

var executingEvent; function someFunction() { ... if(existUnsavedChanges) { showConfirmMessage(); executingEvent = window.event; if (executingEvent.stopPropagation) { executingEvent.stopPropagation(); } else { executingEvent.cancelBubble = true; } ... } } 

Is there a way to do something similar later?

 raise (executingEvent); 

It sounds a little complicated, I would also welcome other options :)

+4
source share
2 answers

to start using the event

elem.dispatchEvent(event)

Where elem is either the element that you bound, or below it in the DOM (so it bubbles up).

Of course, if you have already stopped distributing, the event will not bubble, so you might want to create a new event object.

 var ev = document.createEvent("Event"); ev.initEvent(type, true, true); ev.origEv = originalEvent; elem.dispatchEvent(ev); 
+2
source

It sounds to me like you are overthinking - you can simply pick up the same type of event as the source event (which you would be canceled) after the user has taken the action that you are prompted to enter.

You can determine that the source event was raised by checking the properties of the event object, for example. event type, original target, etc.

0
source

Source: https://habr.com/ru/post/1379288/


All Articles