Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type error: event is undefined in firefox about javascript

Here is the thing. I'm need to open a new tab and draw something on the new opened tab.

I add event listener like that:

div3.addEventListerner("onmousedown",MouseDown(NewWindow.event),false);

But the firefox throw errors about the code in the MouseDown() function when the page is loading. The error is not throwed when I move the mouse.

function MouseDown(event)
{
if(!event)
    {
    var event = window.event;
    }

X = event.pageX;//Throw error here.
Y = event.pageY;

So, there is anyone who knows how to fix this Problem?????

like image 547
cui Avatar asked Apr 15 '26 07:04

cui


1 Answers

Remove the var from var event = window.event. The variable is already declared (as an argument), so re-declaring it with var can only lead to problems.

To be specific, due to hoisting, here is what your code boils down to:

function MouseDown(event) {
    var event; // = undefined
    if( !event) { // always true
        event = window.event; // undefined in modern browsers
    }
    X = event.pageX; // ERROR!
}

Without the var, all is well!

like image 111
Niet the Dark Absol Avatar answered Apr 16 '26 22:04

Niet the Dark Absol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!