S
S
Smirniy_922021-07-11 10:00:23
JavaScript
Smirniy_92, 2021-07-11 10:00:23

How to track leaving the site in the manner of VK?

I'm doing a pet project, in fact, I want to secure the user and add tracking of all links, and if the host of this link does not match the host of my site, I'll give a warning. In principle, I understand how to do it, the key difficulty arises at the very first stage - click tracking. Unfortunately, at the moment I can’t give code examples, due to the fact that I’m on the phone, but simply put, I do this: (I’m not sure that I wrote it correctly from memory, but he understands me and writes the object to the variable with a list of links on the page). And then I do
let links = document.getElementsByTagsName('a')

links.addEventListener('click', function () {
// и здесь код, который будет предупреждать юзера 
});

The debugger in the browser writes that the event handler is not a function. To be honest, I don’t know what to do, because I’m learning back and I don’t really want to go deep into js right now, but just understand the algorithm for attaching an event to a tag. Thank you in advance for your help .
Ps or maybe there is a better solution for my question?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
ninja op op, 2021-07-11
@kur4chyt

let links = document.getElementsByTagName('a')

links.forEach(link => link.addEventListener('click', () => {}))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question