Swapping the onClick method

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

    Swapping the onClick method

    I need to swap the onclick method bound to a layer (nLayer). The
    assignment,

    document.getEle mentById('nLaye r').onclick = nullScript; // OK

    works when the method bound (i.e. nullScript) requires no arguments.
    However, when the method to be bound (i.e. activeScript) DOES require
    an argument as show below,

    document.getEle mentById('nLaye r').onclick = activeScript(1) ; // ERROR

    my browser (IE6.0) reports an error. Is this the correct way to
    perform the such an assignment?

    ////// Function Declarations /////

    function nullScript(){
    //DO STUFF
    ]

    function activeScript(va r number){
    //DO SOMETHING ELSE
    ]

    /////////////////////////////////


    ThanX

    - Olumide
  • Lasse Reichstein Nielsen

    #2
    Re: Swapping the onClick method

    50295@web.de (Olumide) writes:
    [color=blue]
    > I need to swap the onclick method bound to a layer (nLayer). The
    > assignment,[/color]

    What is a "layer"? I will assume you don't mean the <layer> element
    from Netscape 4, but just a plain <div> element.
    [color=blue]
    > document.getEle mentById('nLaye r').onclick = nullScript; // OK
    >
    > works when the method bound (i.e. nullScript) requires no arguments.
    > However, when the method to be bound (i.e. activeScript) DOES require
    > an argument as show below,
    >
    > document.getEle mentById('nLaye r').onclick = activeScript(1) ; // ERROR[/color]

    Yes, you call the function and assing the return value, you don't
    assign the function itself.
    [color=blue]
    > my browser (IE6.0) reports an error.[/color]

    The error would be nice to have, it would remove the need for guessing,
    and thereby the risk of guessing wrong.
    [color=blue]
    > Is this the correct way to perform the such an assignment?[/color]

    Not if I guess what you want correctly. You would probably want to do:
    document.getEle mentById('nLaye r').onclick =
    function() {activeScript.c all(this,1);}

    Remember that all other browsers sends an argument to the handler: the
    event being handled.
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Olumide

      #3
      Re: Swapping the onClick method

      > What is a "layer"? I will assume you don't mean the <layer> element[color=blue]
      > from Netscape 4, but just a plain <div> element.[/color]

      I'm using the plain <div> element.
      [color=blue]
      > The error would be nice to have ...[/color]

      All I got was an "Error on page message" (with the yellow caution in
      the status bar. The only error reported was an "Object required"
      messge, Code 0). In the NN 6 JavaScript console however, I got the
      message: document.getEle mentById(layerI D) has no properties. ... Not
      very helpful.

      Perhaps I should explain what I'm trying to do. My page uses animated
      icons that shrink and disappear when you click them, while the
      previously selected icons "pops" out. What I'm trying to do is to
      prevent the user from clicking on a new icon before the current icon
      disappers, or at least make such clicks "impotent". My plan is to
      (temporarily) replace all onclick handers with a dummy script while
      the clicked icon shrinks. The onclick handler is to be replaced with
      the former handler once the animation is complete.

      Please tell me I'm making sense ...

      - Olumide

      Comment

      • Randy Webb

        #4
        Re: Swapping the onClick method

        Olumide wrote:[color=blue][color=green]
        >>What is a "layer"? I will assume you don't mean the <layer> element
        >>from Netscape 4, but just a plain <div> element.[/color]
        >
        >
        > I'm using the plain <div> element.
        >
        >[color=green]
        >>The error would be nice to have ...[/color]
        >
        >
        > All I got was an "Error on page message" (with the yellow caution in
        > the status bar. The only error reported was an "Object required"
        > messge, Code 0). In the NN 6 JavaScript console however, I got the
        > message: document.getEle mentById(layerI D) has no properties. ... Not
        > very helpful.
        >
        > Perhaps I should explain what I'm trying to do. My page uses animated
        > icons that shrink and disappear when you click them, while the
        > previously selected icons "pops" out. What I'm trying to do is to
        > prevent the user from clicking on a new icon before the current icon
        > disappers, or at least make such clicks "impotent". My plan is to
        > (temporarily) replace all onclick handers with a dummy script while
        > the clicked icon shrinks. The onclick handler is to be replaced with
        > the former handler once the animation is complete.
        >
        > Please tell me I'm making sense ...
        >
        > - Olumide[/color]

        Have a global variable that tracks when one icon is shrinking or
        enlarging. Then, whenever an icon is clicked - set the variable. When
        its finished shrinking/enlarging, reset it. Have the function check that
        variable and if its shrinking/enlarging then return.


        var working = false;
        function shrinkIcon(){
        if (working){
        return;
        }
        else{
        working = true;
        //do your shrinking/enlarging here
        working = false;
        }
        }

        --
        Randy
        Chance Favors The Prepared Mind
        comp.lang.javas cript FAQ - http://jibbering.com/faq/

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Swapping the onClick method

          Olumide wrote:
          [color=blue]
          > The problem is that I am using the setTimeout("fun ction()", timeStep)
          > method - to perform the animation. When I set timeStep to a
          > relatively large value such as 50ms, the code works. When however I
          > set it much lower, the code does not work! AARRGHHHH!!!
          >
          > Anyone got ideas?[/color]

          Do not use timeouts/intervals below 50 ms,
          they are known to be unreliable.


          PointedEars

          Comment

          • Dr John Stockton

            #6
            Re: Swapping the onClick method

            JRS: In article <40744FBB.80905 02@PointedEars. de>, seen in
            news:comp.lang. javascript, Thomas 'PointedEars' Lahn
            <PointedEars@we b.de> posted at Wed, 7 Apr 2004 21:00:11 :[color=blue]
            >Olumide wrote:
            >[color=green]
            >> The problem is that I am using the setTimeout("fun ction()", timeStep)
            >> method - to perform the animation. When I set timeStep to a
            >> relatively large value such as 50ms, the code works. When however I
            >> set it much lower, the code does not work! AARRGHHHH!!!
            >>
            >> Anyone got ideas?[/color]
            >
            >Do not use timeouts/intervals below 50 ms,
            >they are known to be unreliable.[/color]

            Is there independent evidence for that?

            Remember that timeouts will be inexact, since they can only fire in a
            timer tick, which is 54.9 ms on some systems and 10 ms on others; that
            is not unreliability, but may be unfitness for purpose.

            Again, the predecessor posts are ancient; I retain for 17 days at
            present, and no longer have them. A dated attribution line would have
            made the matter clear (Google M-IDs include YYMMDDhhss).

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
            Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
            PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
            Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

            Comment

            Working...