am I being stupid?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • harry

    am I being stupid?

    Trying to get a function working in IE 5.5 (sp3) & Mozilla1.6

    The function is called when a table row is double clicked i.e...

    function dblClicked()
    {
    getElement("vie wSearch").click ();
    }

    This works fine under IE but not Mozilla, so I thought lets break the
    statement down like so -

    function dblClicked()
    {
    var x = getElement("vie wSearch");
    x.click():
    }

    This now doesn't work in either!

    getElement() looks like this -

    function getElement(id)
    {
    var e = document.getEle mentById(id);

    if(e)
    {
    return e;
    }
    else
    {
    alert(e + " not found!");
    }
    }

    Any ideas? - surely they are identical?

    thanks

    harry


  • Michael Winter

    #2
    Re: am I being stupid?

    On Mon, 26 Apr 2004 09:11:35 GMT, harry <a@abc.com> wrote:
    [color=blue]
    > Trying to get a function working in IE 5.5 (sp3) & Mozilla1.6[/color]

    I see two problems:
    [color=blue]
    > The function is called when a table row is double clicked i.e...
    >
    > function dblClicked()
    > {
    > getElement("vie wSearch").click ();
    > }[/color]

    1) Table rows do not have click() methods.

    Microsoft might like to think differently (not surprising, really), but
    most browsers will follow the DOM and only supply click() methods where it
    makes sense: form controls. If you want to call click event listeners,
    either dispatch a click event to the appropriate element, or call the
    function referenced by onclick directly. The most appropriate will depend
    on how you've added listeners.
    [color=blue]
    > This works fine under IE but not Mozilla, so I thought lets break the
    > statement down like so -
    >
    > function dblClicked()
    > {
    > var x = getElement("vie wSearch");
    > x.click():
    > }[/color]

    2) You're not programming very defensively.

    Your getElement() function might return undefined or null, but you don't
    test for it. Make sure you do!

    var x = getElement( 'viewSearch' );
    if( x ) {
    // Use x
    }
    [color=blue]
    > This now doesn't work in either!
    >
    > getElement() looks like this -
    >
    > function getElement(id)
    > {
    > var e = document.getEle mentById(id);
    >
    > if(e)
    > {
    > return e;
    > }
    > else
    > {
    > alert(e + " not found!");
    > }
    > }[/color]

    A better way to write this would be:

    var getElement;
    if( document.getEle mentById ) {
    getElement = function( id ) {
    return document.getEle mentById( id );
    };
    } else if( document.all ) {
    getElement = function( id ) {
    return document.all[ id ];
    };
    } else {
    getElement = function() {
    return null;
    };
    }

    By the way, your alert doesn't make much sense, but even if it did, it
    wouldn't be of much use to the user. If e is null of undefined, your
    message will display: "undefined not found!" That means nothing to you or
    anyone else. Even if you change it to: alert( id + ' not found!'), it's
    useless for the user.
    [color=blue]
    > Any ideas? - surely they are identical?[/color]

    Abandon the direct click() approach as it won't be supported by that many
    browsers. If you have trouble with the indirect method, do ask for more
    help.

    Good luck,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • harry

      #3
      Re: am I being stupid?

      Thanks for that Michael, don't suppose you would have some sample code to
      "dispatch a click event to the appropriate element" ?

      cheers

      harry

      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
      news:opr6177je1 5vklcq@news-text.blueyonder .co.uk...[color=blue]
      > On Mon, 26 Apr 2004 09:11:35 GMT, harry <a@abc.com> wrote:
      >[color=green]
      > > Trying to get a function working in IE 5.5 (sp3) & Mozilla1.6[/color]
      >
      > I see two problems:
      >[color=green]
      > > The function is called when a table row is double clicked i.e...
      > >
      > > function dblClicked()
      > > {
      > > getElement("vie wSearch").click ();
      > > }[/color]
      >
      > 1) Table rows do not have click() methods.
      >
      > Microsoft might like to think differently (not surprising, really), but
      > most browsers will follow the DOM and only supply click() methods where it
      > makes sense: form controls. If you want to call click event listeners,
      > either dispatch a click event to the appropriate element, or call the
      > function referenced by onclick directly. The most appropriate will depend
      > on how you've added listeners.
      >[color=green]
      > > This works fine under IE but not Mozilla, so I thought lets break the
      > > statement down like so -
      > >
      > > function dblClicked()
      > > {
      > > var x = getElement("vie wSearch");
      > > x.click():
      > > }[/color]
      >
      > 2) You're not programming very defensively.
      >
      > Your getElement() function might return undefined or null, but you don't
      > test for it. Make sure you do!
      >
      > var x = getElement( 'viewSearch' );
      > if( x ) {
      > // Use x
      > }
      >[color=green]
      > > This now doesn't work in either!
      > >
      > > getElement() looks like this -
      > >
      > > function getElement(id)
      > > {
      > > var e = document.getEle mentById(id);
      > >
      > > if(e)
      > > {
      > > return e;
      > > }
      > > else
      > > {
      > > alert(e + " not found!");
      > > }
      > > }[/color]
      >
      > A better way to write this would be:
      >
      > var getElement;
      > if( document.getEle mentById ) {
      > getElement = function( id ) {
      > return document.getEle mentById( id );
      > };
      > } else if( document.all ) {
      > getElement = function( id ) {
      > return document.all[ id ];
      > };
      > } else {
      > getElement = function() {
      > return null;
      > };
      > }
      >
      > By the way, your alert doesn't make much sense, but even if it did, it
      > wouldn't be of much use to the user. If e is null of undefined, your
      > message will display: "undefined not found!" That means nothing to you or
      > anyone else. Even if you change it to: alert( id + ' not found!'), it's
      > useless for the user.
      >[color=green]
      > > Any ideas? - surely they are identical?[/color]
      >
      > Abandon the direct click() approach as it won't be supported by that many
      > browsers. If you have trouble with the indirect method, do ask for more
      > help.
      >
      > Good luck,
      > Mike
      >
      > --
      > Michael Winter
      > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]


      Comment

      • Michael Winter

        #4
        Re: am I being stupid?

        On Tue, 27 Apr 2004 11:02:35 GMT, harry <a@abc.com> wrote:
        [color=blue]
        > Thanks for that Michael, don't suppose you would have some sample code to
        > "dispatch a click event to the appropriate element" ?[/color]

        Not off-hand, no.

        The first technique, a direct call, is the easiest and most compatible
        approach. If you attached an event listener using either event properties
        (element.onclic k, etc.) or HTML attributes you write, quite simply:

        element.eventNa me();

        Using an example from your original post, would call the click event
        listener by

        var e = getElement( 'viewSearch' );
        if( e && e.onclick ) {
        e.onclick();
        }

        However, there is a caveat: any listener you call in this fashion must not
        try to access an event object as there won't be one. Most of the time,
        this won't be a problem, but do be careful.

        The second technique, dispatching an event, is necessary if you use
        Microsoft's attachEvent() method or the DOM's addEventListene r() method,
        but it requires more recent browsers and a lot more work. I'd prefer not
        to include details of this at the moment, but if you need more
        information, ask.

        [snip]

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        Working...