javascript - window.addEventListener('message', myFunction(event)) doesn't work -


i'm trying understand why following not work.

var myfunction = function(event) {     // event };  window.addeventlistener('message', myfunction(event)); 

i following error: "referenceerror: event not defined".

however, following works , event able used.

window.addeventlistener('message', function(event) {     // event }); 

how can use event in first situation? why event accessible in second situation?

you seeing error because invoking function immediately. need pass reference function instead.

in other words, change this:

window.addeventlistener('message', myfunction(event)); 

to this:

window.addeventlistener('message', myfunction); 

when using addeventlistener() method, event object passed first parameter default when event fired.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -