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,753
  • 72
  • 57
  • 129
AngryHacker
  • 59,598
  • 102
  • 325
  • 594

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,450
  • 15
  • 93
  • 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
  • 54,171
  • 16
  • 118
  • 151
0
$(document).click(function(){});
Jake Kalstad
  • 2,045
  • 14
  • 20
0
document.onclick = function() { alert("hello"); };

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

cobbal
  • 69,903
  • 20
  • 143
  • 156