Discussions » Development

Delete event listener upon activation

§
Posted: 2024-04-06

Greetings!

I am attempting to remove the event listener once it is called.

document.addEventListener("DOMContentLoaded", function() {
  document.body.addEventListener("mouseover", async function(e) {
    if (e.target && e.target.nodeName == "A") {
      hyperLink = e.target;

      // Attempts to remove event listener
      // /questions/4386300/javascript-dom-how-to-remove-all-event-listeners-of-a-dom-object

      //hyperLink.removeEventListener('mouseover', addEventListenerToHyperlinks);
      //hyperLink.replaceWith(hyperLink.cloneNode(true));
      //let clone = hyperLink.cloneNode(true);
      //hyperLink.replaceWith(clone);

      await xhrHyperLink(hyperLink);
    }
  });
});

Whoever sees this, please consider helping.

§
Posted: 2024-04-06

add }, { once: true }); in the end

§
Posted: 2024-04-07

It does not work:

document.addEventListener("DOMContentLoaded", function() {
  document.body.addEventListener("mouseover", async function(e) {
    if (e.target && e.target.nodeName == "A") {
      hyperLink = e.target;
      await xhrHyperLink(hyperLink);
  }, { once: true });
});

When adding { once: true } to event mouseover the event does not work - at all, and adding { once: true } to event DOMContentLoaded makes no change.

§
Posted: 2024-04-07

It should work though

https://streamable.com/midyv7

§
Posted: 2024-04-07

I have added { once: true } to Proxy Redirect. Please see for yourself, that the functionality to change links, as shown in the video is not working.

In the video, I present the issue caused by the event being executed more than once.

§
Posted: 2024-04-07

Answering to myself: Of course it does not work for links, because the "event listener" is directed at the element "body" document.body.addEventListener.

§
Posted: 2024-04-07

Issue has been solved!

      for (linkElement of document.links) {
        linkElement.addEventListener("mouseover",
          async function(e) {
            if (e.target && e.target.nodeName == "A") {
              hyperLink = e.target;
              await xhrHyperLink(hyperLink);
            }
          },
          {
            once: true
          }
        );
      }

Thank you hacker09 and Konf!

Post reply

Sign in to post a reply.