Results 1 to 5 of 5

Thread: Would this work??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Would this work??

    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?

    Code:
    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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    function foo(event) {
      alert(event.clientX);
    }
    
    some_element.onmousedown = function(e) {
      foo(e || window.event);
    };
    ... but this is less efficient than the traditional approach:
    Code:
    function foo(e) {
      var ev = e || window.event;
      alert(ev.clientX);
    }
    
    some_element.onmousedown = foo;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: Thanks Twey!! Would this work then?(I know it won't but I think it should)

    Code:
    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.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi View Post
    Would this work then?(I know it won't but I think it should)

    Code:
    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.
    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.

    Code:
    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

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    mwinter: Thanks again for the explanation and your help. Thanks a billion!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •