Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    UK, South Wales, Newport
    Posts
    21
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Javascript event handlers

    can someone please explain the code in this link. Just this small bit since i seen it in a few different places.
    it starts off with if (!e) ...
    please explain wth that means
    thank you so much

    heres the link - http://www.w3schools.com/js/tryit.as...ent_srcelement

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    In that case e is the parameter to the event handler whichElement(). But depending on the browser, it will not be handed over automatically. E.g. in Internet Explorer, it will not be there. In that case e will be undefined. So if(!e) means "if e is undefined" ... on Internet Explorer this condition will be met and e is assigned with the global event object instead.

  • Users who have thanked devnull69 for this post:

    samz (09-15-2011)

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    UK, South Wales, Newport
    Posts
    21
    Thanks
    8
    Thanked 0 Times in 0 Posts
    thanks. that helped. i see now.

    if you dont mind, would you be able to explain each line of the code for me in basic pseudo code/rough english?

    If you dont mind

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,519
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Code:
    // declare a function which takes one argument
    // *if* the argument is passed to the function, the variable e will be non-null
    function whichElement(e)
    {
        // declare a variable to be defined later
        var targ;
        // find out if [b]e[/e] has a value in it
        // personally, I *DO NOT LIKE* seeing this coded the way it is
        // because it doesn't make it clear what is really going on
        // I would have coded it as
        //       if ( e == null )
        if (!e)   /* means the same thing as my code */
        {
            // if no argument was passed to the function,
            // then assume that the browser is MSIE 8 and before
            // and get the builtin global event from MSIE:
            var e=window.event;
        }
        // find out if the object referenced by e has a property named target
        // again, I think it would be clearer to write the next line as
        //      if ( e.target != null )
        if (e.target)
        {
            // okay, so there is such a property...assign it to the targ variable
           targ=e.target;
        }
        // if the variable doesn't have a target property, 
        // maybe it has a srcElement property
        // and, again, it would be clearer to write it as
        //    if ( e.srcElement != null )
        else if (e.srcElement)
        {
            // okay, so there is such a property...assign it to the targ variable
            targ=e.srcElement;
        }
        // as the comment says, the Safari browser has a bug
        if (targ.nodeType==3) // defeat Safari bug
        {
            // so for it we really want to get the parent of the target that Safari found
            targ = targ.parentNode;
        }
        // declare another variable
        var tname;
        // find out what kind of element (tag) we found as the target
        tname=targ.tagName;
        // and give a message
        alert("You clicked on a " + tname + " element.");
    }
    What's wrong with that code (other than my objections to its coding style)?

    Well, suppose that the function is called with an argument that is *NOT* an object that has either a target or srcElement property? Then the varible targ will never get initialized and so when we get to the targ.tagName we get an error, because there is never any property on a null object.

    So this function should *only* be called from the onclick of some tag.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    samz (09-15-2011)


  •  

    Posting Permissions

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