PDA

View Full Version : Would this work??



shachi
12-21-2006, 02:54 PM
Hello everyone,

I was experimenting with events when I came up with a function, but I am not sure if it will work on IE and on FF so could anyone tell me if this will work?



function foo(event){
alert(event.clientX);
}

// to call the function

some_element.onmousedown = foo(window.event);


EDIT: Nevermind, I just found out that it doesn't even work in FF, Thanks anyways. ddadmin: Please feel free to delete this post.

Twey
12-21-2006, 05:42 PM
I'd be surprised if it worked at all. It's syntactically correct, but will call the function straight away, then assign its return value to the onmousedown property. foo is a function; foo() is the return value of foo.

You can do something similar to this by using an inline function:
function foo(event) {
alert(event.clientX);
}

some_element.onmousedown = function(e) {
foo(e || window.event);
};... but this is less efficient than the traditional approach:
function foo(e) {
var ev = e || window.event;
alert(ev.clientX);
}

some_element.onmousedown = foo;

shachi
12-21-2006, 06:52 PM
Twey: Thanks Twey!! Would this work then?(I know it won't but I think it should)



function somefunction(event){
alert(event.clientX);
}
some_element.onmousedown = somefunction;


I think it should work as window is a global object due to which we don't need to address it as window.event(like window.alert) so it should work in both IE and FF. But it doesn't. :p

mwinter
12-22-2006, 11:38 AM
Would this work then?(I know it won't but I think it should)



function somefunction(event){
alert(event.clientX);
}
some_element.onmousedown = somefunction;


No. The argument will obscure MSIE's global event object.

The formal argument, event, will be added to the variable object - the object that holds local variables and nested function declarations - which in turn is added to the top of the scope chain for the function, somefunction. When the script engine attempts to resolve the identifier, event, against the scope chain, it will find it immediately rather than reaching the global object. Even though this variable will be undefined in IE, it is still the result of the resolution process.



I think it should work as window is a global object due to which we don't need to address it as window.event(like window.alert) so it should work in both IE and FF. But it doesn't. :p

The window object is implemented in browsers as a reference to the global object. Global variables and functions are essentially properties of this object, so they can often be found through identifier resolution as the global object is always in the scope chain. However, it is always the last object on the scope chain. This means that other objects that occur higher up in the chain can hide global variables.



var x = 'foo',
y = 'bar';

function a() {
var x = 'baz';

alert(x + ' ' + y);
}
function b() {
alert(x + ' ' + y);
}

a(); // baz bar
b(); // foo bar

With the first function, a, the variable object has its own property, x. Variable resolution will find this rather than the global. In the second, b, the only property of variable object is named 'arguments': the arguments object (which exists in all functions).

The best solution is what Twey posted: reference the event object through the global object, explicitly.

Mike

shachi
12-22-2006, 03:12 PM
mwinter: Thanks again for the explanation and your help. Thanks a billion!!