'directory of files in an iFrame: get url of file clicked

In an iFrame (same origin), I'm displaying a list of file-links, like this:

<iframe src="my_public_folder_of_files/"></iframe>

Then, in my javascript, I'm trying to listen for the click event on those links, so I can get the href/url of the chosen file, like this

window.frames[0].document.addEventListener('click', e => {
  e.preventDefault();
  console.log(e.target.href);
})

But I'm not getting any logging at all.

If I inspect the page, I can see the <a href="file_url"> elements there, but I can't seem to access them programatically.

How can I get the url of a link clicked within a iFrame that's displaying a same-origin directory?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source