0

I am new to Javascript and jQuery so i have very silly doubt please bear with me

$(document).click(function () {
    alert("!");
    var e = event || window.event;  
    alert("!");
});

if i click on any part of the webpage then i should get two alerts with "!" as the text, it is working as expected in Chrome. In Firefox it is alerting only once

http://jsfiddle.net/Wu2Gh/8/

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Aditya
  • 300
  • 2
  • 3
  • 15
  • 2
    event is not defined, and jquery does that check for you so it is a waste anyway. Look at the documentation: http://api.jquery.com/click/ – epascarello May 15 '14 at 19:50
  • 1
    check your JS console, you'll see error messages. – Marc B May 15 '14 at 19:51
  • updated your fiddle http://jsfiddle.net/Wu2Gh/11/ – Mark Rijsmus May 15 '14 at 19:53
  • @epascarello So wat should i do know which element was clicked. But i have read in other Stack overflow question that this statement should work in both firefox and chrome http://stackoverflow.com/questions/14051936/javascript-event-target-not-working-in-mozilla – Aditya May 15 '14 at 19:53
  • 1
    That check you would have to do if you were not using jQuery. It does that under the covers. It is as simple as `$(document).click(function (e) { console.log(e.target); });` – epascarello May 15 '14 at 19:56

5 Answers5

3

Since you use jQuery, all you need to do is:

$(document).click(function (event) {

});

jQuery handles the browser differences and always passes an event object to the handler.

I recommend to read the jQuery tutorial about event handling.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

There is no event variable you are referring to. The Event object is the argument to the handler function. It should be

$(document).click(function (event) {
    alert("!");
    var e = event || window.event;  
    alert("!");
});
marekful
  • 14,986
  • 6
  • 37
  • 59
0

try this.you are not passing event parameter to function

$(document).click(function (event) {
    alert("!");
    var e = event || window.event;  
     alert("!");
});
santosh singh
  • 27,666
  • 26
  • 83
  • 129
0

You have an error "event is not defined" why dont you get the event this way:

$(document).click(function (event) {
  alert("!");
  var e = event || window.event;  
  alert("!");
});
Alex Shilman
  • 1,547
  • 1
  • 11
  • 15
0

The event is obviously undefined. So that line of code is causing a stop. You need to send the event to the click function.

$(document).click(function(event){


});
Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54