2

Just like the question states. I want to fire off an event that calls a method everytime the user clicks on the web page.

How do I do that without use of jQuery?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
AngryHacker
  • 56,860
  • 95
  • 305
  • 561

4 Answers4

4

Without using jQuery, I think you could do it like this:

if (document.addEventListener) {
    document.addEventListener('click',
        function (event) {
            // handle event here
        },
        false
    );
} else if (document.attachEvent) {
    document.attachEvent('onclick',
        function (event) {
            // handle event here
        }
    );
}
Greg
  • 33,130
  • 15
  • 90
  • 100
2

Here's one way to do it..

if (window.addEventListener)
{    
    window.addEventListener('click', function (evt)
    {
        //do something
    }, false);
} 
else if(window.attachEvent)
{
    window.attachEvent('onclick', function (evt)
    {
        // do something (for IE)
    });
}
Mike Dinescu
  • 51,717
  • 14
  • 111
  • 144
0
$(document).click(function(){});
Jake Kalstad
  • 1,977
  • 13
  • 20
0
document.onclick = function() { alert("hello"); };

note that this will only allow for one such function though.

cobbal
  • 68,517
  • 19
  • 142
  • 155