Determine click target using JavaScript?

How to determine which object or identifier or user has right-clicked? I use onContextMenu to run the function, but I do not know how to determine the purpose.

+3
source share
3 answers
<html>
<head>
<script type="text/javascript">
    if (document.addEventListener) {
        document.addEventListener('contextmenu', function(e) {
            alert(e.target.nodeName); //or e.target.getAttribute('id') 
            e.preventDefault();
        }, false);
    } else {
        document.attachEvent('oncontextmenu', function(e) {
            alert(window.event.srcElement.nodeName); //or e.srcElement and then like above
            window.event.returnValue = false;
        });
    }
</script>
</head>
<body>
<span>Lorem ipsum...</span><br/>
body content
</body>
</html>

PS. I have seen similar code before;)

+5
source

Your handler should accept the event object as its parameter; The srcElement property of the event will be the object that raised the event.

0
source

Patrick, onContentMenu, , . .

var oE = event.srcElement || event.originalTarget;

Note: originalTarget is specific to Mozilla. You might want to pay attention to event.target https://developer.mozilla.org/en/DOM/event.target

0
source

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


All Articles