I'm trying to get link (url of clickable elements) with the left mouse click, next method doesn't work for all elements:
function callback(e) {
if (e.button != 0 ) {
return;
}
alert(e.target.href);
}
document.addEventListener('click', callback, true);
For example for some elements on Youtube site - titles or thumbnails (all of them are clickable and they lead to some video/playlist):
href is undefined
but it's clickable and Google Chrome browser shows a preview of link to which this element leads:
UPDATE
The problem that some A tags on Youtube site wraps other elements inside them: <a href="..."><span>...<span/><div.......></a>
I tried debug mode (inspect), selected some of such element to inspect and it picked <span>
inside of <a>
.
Additional Solution: https://jsfiddle.net/z2huqjjh/2/ (will be good solution if links (A tags) are dynamically being added to a page)