Can I assign an event to a global variable?

Discussion in 'Javascript' started by TriFuFoos@gmail.com, Oct 4, 2007.

  1. Guest

    Hi there, I was wondering if anyone knew if/how to assign an event to
    a global variable?

    I tried to do it and IE 7 came back with an error saying "Member not
    found"
    My code looked similar to the following:

    var globalEvevnt;
    function showPopup(event){
    globalEvent = event;
    alert(globalEvent.type);
    setTimeout(function(){unhideDiv()}, 2000 );

    }



    function unhideDiv(){
    alert(globalEvent.type); //Member not found error found on this
    line
    }

    I was wondering if I had declared the globalEvent = new Object();
    would that make any difference? I thought everything in JS was an
    object
    so the event could be stored to one as well?

    If anyone could shed some light on this issue it'd be greatly
    appreicated thanks!
     
    , Oct 4, 2007
    #1
    1. Advertising

  2. wrote:
    > I tried to do it and IE 7 came back with an error saying "Member not
    > found"
    > My code looked similar to the following:


    Posting only similar code is likely not to be helpful in analyzing the
    problem. Post exactly what you use instead, a URL for a test case if necessary.

    > var globalEvevnt;
    > function showPopup(event){
    > globalEvent = event;


    You have not showed how showPopup() is called. If I assume that you have
    used the Function object reference in an event handler assignment or in an
    event listener addition call, the described problem could be explained as
    follows:

    Declaring `event' as a named argument of showPopup() modifies scope chain
    resolution of `event' within showPopup to result in a reference to that
    argument, and not to an Event object that may be later in the scope chain.
    However, in the MSHTML DOM the Event object is not passed to the event
    listener, and so `event' yields `undefined'. And `undefined' has no properties.

    This can be fixed with

    if (! event) event = window.event;

    I recommend to use `e' instead of `event' for the argument identifier to
    avoid confusion.

    > alert(globalEvent.type);


    Given the above assignment, this should throw a TypeError exception already
    in the MSHTML DOM.

    > setTimeout(function(){unhideDiv()}, 2000 );


    Should be

    window.setTimeout(...);

    > }
    >
    > function unhideDiv(){
    > alert(globalEvent.type); //Member not found error found on this
    > line


    window.alert(...);

    Code should be posted so that it is unlikely to break on execution when
    being wrapped to about 72 characters per line. Therefore, multi-line
    comments are to be used instead of single-line comments in that case, unless
    the comment is short enough.

    > }
    >
    > I was wondering if I had declared the globalEvent = new Object();

    <> would that make any difference?

    Of course not. The reference to the newly created Object object would have
    been overwritten by

    globalEvent = event;

    and the Object object would have been marked for Garbage Collection as there
    would have been no more references to it.

    > I thought everything in JS was an object


    Not everything; there are primitive data types as well. However, such a
    value is not involved here.

    > so the event could be stored to one as well?


    You can store a reference to an (Event) object in a variable. The provision
    for that is that there is an (Event) object in the first place.

    > If anyone could shed some light on this issue it'd be greatly
    > appreicated thanks!



    HTH

    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Oct 4, 2007
    #2
    1. Advertising

  3. Guest

    Thanks for your reply Thomas!

    I'm not very knowledgeable with how event listeners or handlers work?
    What difference does window.setTimeout as opposed to setTimeout do?

    and I'm calling the showPopup like this:

    <td onmouseover="showPopup(event);"></td>


    What I'm also confused about is although you say
    alert(globalEvent.type) should
    give me an error, it works when I'm still in the showPopup method, and
    yet
    it fails when I call it again in unhideDiv()??

    Thanks in advance!
     
    , Oct 4, 2007
    #3
  4. wrote:
    > I'm not very knowledgeable with how event listeners or handlers work?


    Is that a question?

    > What difference does window.setTimeout as opposed to setTimeout do?


    setTimeout() is not a built-in method; it is a method of Window host objects
    and it should be called so.

    One is calling a known and well-supported (albeit proprietary)
    function-property of the object `window' refers to, the other is calling a
    property of the next object in the scope chain that has such a property,
    provided that there is such an object and that this property can be called
    (otherwise it throws a TypeError exception). In short, the former is more
    obvious (as one can see at a glance by/for which object the method is
    called), more efficient (as scope chain resolution is faster with a given
    object reference), and less error-prone.

    > and I'm calling the showPopup like this:
    >
    > <td onmouseover="showPopup(event);"></td>


    That changes the meaning of the `event' named argument of showPopup(), of
    course. I assumed that you used (DOM Level 0)

    refToTDElObj.onmouseover = showPopup;

    or (W3C DOM Level 2 Events)

    refToTDElObj.addEventListener('...', showPopup, ...);

    or (MSHTML DOM)

    refToTDElObj.attachEvent('...', showPopup);

    instead. As I said, how showPopup() is called is significant.

    > What I'm also confused about is although you say
    > alert(globalEvent.type) should
    > give me an error, it works when I'm still in the showPopup method, and
    > yet it fails when I call it again in unhideDiv()??


    The reason for that is if you pass `event' in an event handler attribute
    value, it refers (proprietarily) to the current Event object. Which means
    that in the MSHTML DOM the value of the `event' argument (in showPopup) is
    not `undefined'. Hence globalEvent.type there does not throw an exception.

    The only explanation I have as to why the same lookup threw an exception in
    unhideDiv() is that you have indeed a typo there --

    | var globalEvevnt;

    -- and that this typo is significant. Because that would mean that

    globalEvent = event;

    in showPopup() would attempt to add a property to an object in the scope
    chain, which is not necessarily the Global Object. And then in unhideDiv(),

    | globalEvent.type

    would be resolved to

    undefined.type

    which would throw a TypeError. Unless you have posted code less similar to
    the code you are actually using.

    I recommend instead not to use globally available properties. Quickhack:

    function unhideDiv(e)
    {
    window.alert(e.type);
    }

    function showPopup(e)
    {
    window.alert(e.type);
    window.setTimeout(function(){ unhideDiv(e); }, 2000);
    }


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Oct 5, 2007
    #4
  5. Lee Guest

    said:
    >
    >Thanks for your reply Thomas!
    >
    >I'm not very knowledgeable with how event listeners or handlers work?
    >What difference does window.setTimeout as opposed to setTimeout do?
    >
    >and I'm calling the showPopup like this:
    >
    ><td onmouseover="showPopup(event);"></td>
    >
    >
    >What I'm also confused about is although you say
    >alert(globalEvent.type) should
    >give me an error, it works when I'm still in the showPopup method, and
    >yet
    >it fails when I call it again in unhideDiv()??


    The problem is that when you assign an object to a variable,
    you're really only assigning a reference to that object, not
    making a copy of it. So the variable can only be used until
    a new event occurs, replacing the event object with a new one.


    --
     
    Lee, Oct 5, 2007
    #5
  6. On Oct 4, 3:13 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > wrote:
    > > I tried to do it and IE 7 came back with an error saying "Member not
    > > found"
    > > My code looked similar to the following:

    >
    > Posting only similar code is likely not to be helpful in analyzing the
    > problem. Post exactly what you use instead, a URL for a test case if necessary.


    Even better is posting a minimal, 30 lines or less, self-contained,
    complete HTML page example that exhibits the problem and was written
    with posting it to c.l.j in mind. A link to the same example online is
    a nice touch.

    Peter
     
    Peter Michaux, Oct 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    8
    Views:
    53,783
    saravanavsk
    Jan 25, 2010
  2. Nmishra
    Replies:
    3
    Views:
    374
    Default User
    May 24, 2004
  3. Replies:
    18
    Views:
    7,130
    Karl Heinz Buchegger
    Jul 22, 2005
  4. Ranjan
    Replies:
    1
    Views:
    2,792
    Eliyahu Goldin
    Apr 10, 2007
  5. Jase Schick
    Replies:
    11
    Views:
    598
    Geoff
    Aug 5, 2010
Loading...

Share This Page