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

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -