DispatchEvent does not format the event, but returns true

On the MDC site, they have a cool demo of dispatchEvent that works fine in my chrome 15.

I am trying to take an event object and pass it to dispatchEvent, and set up a simple case here where you record events as an array then play them back.

In essence, I set up a window listener for a click, and then a window.dispatchEvent(recordedEvent) .

I cannot determine why my event object from the event listener will not generate the same as the event from initMouseEvent in the MDC example.

I'm not really worried about how this works, I want to know why it doesn't work, when after reading a funny guide it seems to imply that it should be.

+2
source share
1 answer

This seems to work great for me; here is an update.

edit - wait - are you worried that it returns true , as if preventDefault () were being called? If so, then I understand your confusion.

finally edit ok. I think I see the problem. When you dispatch an event, you always dispatch from window . If you track items instead, then it works.

Here is a good code that works (for me in Firefox 7):

 //Vars for the elements we're working with var replay = document.getElementById("replay"); var replaying = false; //Handler to record events into a data array. var handler = function (e) { if (replaying) { console.log("replay " + e.type); } else if (e.target.tagName.toLowerCase() !== 'input') { return; } else { handler.data.push({elem: e.target, event: e}); console.log(handler.data); } }; handler.data = []; //Listen for the click on the replay button replay.addEventListener("click", function(){ //Remove listeners so we don't create some crazy //infinite paradox with turtles all the way down // window.removeEventListener("click", handler); replaying = true; //Clear the textbox out var status = [], obj; //Dispatch a bunch of stored up events for (var i=0; i<handler.data.length;i++){ obj = handler.data[i]; status.push(obj.elem.dispatchEvent(obj.event)); } console.log(status); replaying = false; }); //Listen for some specific events //window.addEventListener("keyup", handler); window.addEventListener("click", handler); 

Also note that it is good to avoid saving click events on the Repeat button itself.

+3
source

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


All Articles