7

I try to find a way to check if an eventListener already exists in a window, I've tried the solution in this similar question and more other solutions, but nothing works for me.

Here is the code:

   checkEvent=function()
   {
    if('keydown' in window)
        console.log('exist');
    else
        console.log('not_exist');
   }

And here is my eventListener that I add on my page Load

document.addEventListener("keydown", PayKeydownHandler);

But the first code, returns 'not_exist' as a result

PS: The function checkEvent(), i call it in onclick of ENTER keydown.

Any help please? Thank's in advance.

4
  • First of all, note that your function looks at window, but you add the event listener to document. Second, if you're only going to add one listener, you could probably just directly set the onkeydown property and then have checkEvent check if that property is set to a function. Otherwise, I don't believe there's a built in way to do this. Commented Dec 6, 2017 at 17:58
  • See the answer from: stackoverflow.com/questions/11430672/… Commented Dec 6, 2017 at 18:01
  • @pushkin you're right, I didn't even pay attention that I'm working with document while, I'm testing on window. But even with that I have the same result. Thank's for your reply Commented Dec 7, 2017 at 10:19
  • @CoursesWeb I have tried this solution too, but the console showed me an error in the syntax. Thank's for your reply Commented Dec 7, 2017 at 10:21

1 Answer 1

-3

try this

jQuery._data( window, "events" );

it work in jquery 1.8 . Read this jQuery blog post.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank's for your reply, but this is not useful in my case.
He never told he was using jQuery...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.