0

In chrome: if i excute this code(javascript)

 function abc(event){
      console.log('hello: '+event);
    }
abc(event);

the above code gives output as : "hello:undefined"

but same code if we excute in firefox we get error what is the simple solution for this.

Vishal Patil
  • 381
  • 3
  • 15
  • But it's not really clear what you expect... The variable `event` doesn't exist when you call `abc`. – Ovidiu Dolha Jul 17 '17 at 14:17
  • I know RefrenceError event is not defined in firefox passing paramerter also does not fixed it. – Vishal Patil Jul 17 '17 at 14:17
  • 2
    Not all browsers have a global `event`, hence it is `undefined` for those browsers and not for the others. – vi5ion Jul 17 '17 at 14:18
  • 2
    The simple solution is to not use references that aren't defined. –  Jul 17 '17 at 14:18
  • @Ovidiu Dolha i need same output as chrome in firefox – Vishal Patil Jul 17 '17 at 14:18
  • 1
    @VishalPatil You're asking for the two different browsers to work the same? Good luck with that. – George Jul 17 '17 at 14:19
  • 1
    Browser not work as per your requirement. You need to write correct code. – Ved Jul 17 '17 at 14:19
  • @ vi5ion yes i know that we should not use this but actually i need event in firefox as in my project i have given it functionality based on event variable. – Vishal Patil Jul 17 '17 at 14:20
  • 2
    Usually when using an `event` in a function, that means the function is actually triggered by an `event` like `click` or `change`. When setting the eventlistener you have the `event` there and you can pass it to another function. – vi5ion Jul 17 '17 at 14:21
  • The solution what i have read is that we need to pass event as parameter in many links.https://stackoverflow.com/questions/20522887/referenceerror-event-is-not-defined-error-in-firefox – Vishal Patil Jul 17 '17 at 14:21
  • i guess you should just do something like abc(this.event) but it's kinda ridiculous – Ovidiu Dolha Jul 17 '17 at 14:21
  • 1
    But when you do that, you pass the function as event handler, not call it yourself. If you want to manually invoke the event, you need to construct an event object first and pass that. –  Jul 17 '17 at 14:22

1 Answers1

0

There is no event happening in your script to track. You are simply calling a method and passing it a property.

If you want to grab "event" then you have to have to create an event.

Example: This is a click event in jQuery.

$(sometarget).click(function(e) {
  console.log(e);
});
Korgrue
  • 3,430
  • 1
  • 13
  • 20