javascript - Intercept all mouse events - Stack Overflow

I'm looking for a way to intercept all mouse events (especially clicks) on a web page.My first ins

I'm looking for a way to intercept all mouse events (especially clicks) on a web page.

My first instinct was $('body').click(....)

But that's not capturing the event if any other Handler is attached for a particular event.

To put the question in context: I'm writing a "statistics" module for my web app. We want to keep track of all user interactions on the pages to adapt. If we see they don't use a functionality we will add a tooltip for example.

I have following frameworks :

jQuery, Knockout and a little Framework "home made"

What I want is a function which is called on each click with mouse position.

I'm looking for a way to intercept all mouse events (especially clicks) on a web page.

My first instinct was $('body').click(....)

But that's not capturing the event if any other Handler is attached for a particular event.

To put the question in context: I'm writing a "statistics" module for my web app. We want to keep track of all user interactions on the pages to adapt. If we see they don't use a functionality we will add a tooltip for example.

I have following frameworks :

jQuery, Knockout and a little Framework "home made"

What I want is a function which is called on each click with mouse position.

Share Improve this question edited May 6, 2016 at 23:12 Jurion asked Nov 16, 2013 at 3:41 JurionJurion 1,29811 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Try something like this (without jQuery):

    var eventCount = 0;
    var eventProperty = [];

    var TrackMouse = function (mouseEvent) {
        eventProperty[eventCount++] = {
            id: mouseEvent.toElement.id,
            type: 'mouse',
            ts: Date.now(),
            x: mouseEvent.x,
            y: mouseEvent.y
        };

        console.log("Element id: " + eventProperty[eventCount - 1].id + ", X: " + mouseEvent.x + ", Y: " + mouseEvent.y + "\n");
    };

    document.addEventListener('click', TrackMouse);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744311598a4567975.html

相关推荐

  • javascript - Intercept all mouse events - Stack Overflow

    I'm looking for a way to intercept all mouse events (especially clicks) on a web page.My first ins

    7天前
    110

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信