3

I've defined an event like this:

var event = new Event('myevent');

Is there anyway which i can check an event called 'myevent' has been defined ?
I want to have an option like this:

If an event called 'myevent' exists, attach a handler to it, else attach the handler to another event like 'myevent2' !

6
  • 2
    you don't attach listeners to events, you attach them to event targets. Whether an event targets emits a certain kind of event is a completely different story. And you haven't "defined" or "declared" a new event type, you just instantiated a new event instance in the local scope. It does not affect anything else. Commented Apr 23, 2014 at 19:36
  • I agree with Bergi's comments. Your question implies that maybe you don't understand how custom events work. I'd suggest you read this: developer.mozilla.org/en-US/docs/Web/Guide/Events/… Commented Apr 23, 2014 at 19:39
  • ^^^ agree with those two, but you can check if the variable is defined and if the event type is myevent like this if (event && event.type == 'myevent') .... Commented Apr 23, 2014 at 19:49
  • Ok let me be more specific. I've wrote some kind of a plugin called "resizestop" which triggers when the window resizing has been stopped for some timeout. now i want to use this in another plugin. you can somehow say that the newer plugin requires the resizestop plugin. Now i want to check this requirement by checking if the event has been defined. any suggestion (except copy and paste resizestop code on the top of your new plugin) ? and btw thank you all for your responces Commented Apr 24, 2014 at 2:25
  • @adeneo one idea which pass in my head was to create a global variable and assign the event to it. but i'm not a big fan of global variables, thats why i asked it here Commented Apr 24, 2014 at 2:31

1 Answer 1

1

This will check if the event variable is and event object

if(event instanceof Event)
    //All Good lets go
Sign up to request clarification or add additional context in comments.

1 Comment

Actually the event has been created in a function (it has a local scope). but i've never thought of using 'instanceof' keyword, thank you for that ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.