Accessing elements drawn with script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    Accessing elements drawn with script

    I appreciate all the responses to my earlier post about accessing
    named elements. However, I'm still wondering about my actual problem,
    which is that I need to initialize some arrays of named elements when
    the document is loaded so they can be used by other functions used as
    event handlers. What I have now is that every function that requires
    these arrays checks to see whether they have been initialized, and if
    not initializes them. That seems to be a fairly ugly hack, and I'd
    like to use a better way if it exists. Does that make sense?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • RobG

    #2
    Re: Accessing elements drawn with script

    Christopher Benson-Manica wrote:[color=blue]
    > I appreciate all the responses to my earlier post about accessing
    > named elements. However, I'm still wondering about my actual problem,
    > which is that I need to initialize some arrays of named elements when
    > the document is loaded so they can be used by other functions used as
    > event handlers. What I have now is that every function that requires
    > these arrays checks to see whether they have been initialized, and if
    > not initializes them. That seems to be a fairly ugly hack, and I'd
    > like to use a better way if it exists. Does that make sense?
    >[/color]


    How about creating an initArray() function and running it with
    the body onload event?

    Testing to make sure something exists or has a suitable value is not
    a 'hack' but rather good practice that should ensure more robust
    code.

    I would:

    1. Declare the array right at the very top of the page so I know it
    exists.

    2. Create a function that fills the array from a body onload event,
    then I know it will only run after the elements have been created.

    3. For each script that needs to use the array, check the length
    right at the start and return (maybe with some error value) if the
    length is zero.


    If the array is created separately to scripts that use it,
    always check that it exists because scripts (say one activated by an
    onclick) may be triggered before the page finishes loading.

    All that is required to test for the array is something like:

    if (!eleArray || 0 == eleArray.length ) {return;}

    Here's a skeleton of what is proposed above:

    <head><title> blah </title>
    <script type="text/javascript">

    var eleArray = [];

    function someFunction(){
    if (!eleArray || 0 == eleArray.length ) {return;}

    // rest of function...
    }

    function initEleArray(){
    var i,j,k,x;
    for (i=0, j=arguments.len gth; i<j; i++){
    x = document.getEle mentsByName(arg uments[i]);
    k = x.length;
    while (k--) { eleArray.push(x[k]); }
    }
    }

    </script>
    </head>
    <body onload="initEle Array('name1',' name2',...);" ...>

    --
    Rob

    Comment

    • Christopher Benson-Manica

      #3
      Re: Accessing elements drawn with script

      RobG <rgqld@iinet.ne t.auau> spoke thus:
      [color=blue]
      > How about creating an initArray() function and running it with
      > the body onload event?[/color]

      I've been told that elements written with script may not be inserted
      into the document tree by this time, at least not via
      getElementsByNa me().
      [color=blue]
      > Testing to make sure something exists or has a suitable value is not
      > a 'hack' but rather good practice that should ensure more robust
      > code.[/color]

      Well, I guess it isn't a hack if no better way exists...
      [color=blue]
      > 2. Create a function that fills the array from a body onload event,
      > then I know it will only run after the elements have been created.[/color]

      See my first paragraph above.
      [color=blue]
      > 3. For each script that needs to use the array, check the length
      > right at the start and return (maybe with some error value) if the
      > length is zero.[/color]

      That's what I'm doing, although it seems suboptimal.

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • RobG

        #4
        Re: Accessing elements drawn with script

        Christopher Benson-Manica wrote:[color=blue]
        > RobG <rgqld@iinet.ne t.auau> spoke thus:
        >
        >[color=green]
        >> How about creating an initArray() function and running it with
        >> the body onload event?[/color]
        >
        >
        > I've been told that elements written with script may not be inserted
        > into the document tree by this time, at least not via
        > getElementsByNa me().[/color]

        You may have issues if you try to access script-created elements
        before the page has finished loading, however body onload /should/
        only run after the page has finished loading, all elements and their
        attributes should be available.

        Of course, there's bound to be an exception somewhere...

        If your page takes a while to load, you could put script in the page
        just after each element to be added to the array that adds that
        element as soon as it is created.

        Alternatively, set a global flag at the very end of your onload
        function and don't let any script run until it's true.

        Or a mix of the above...
        [color=blue]
        >
        >[color=green]
        >> Testing to make sure something exists or has a suitable value is not
        >> a 'hack' but rather good practice that should ensure more robust
        >> code.[/color]
        >
        >
        > Well, I guess it isn't a hack if no better way exists...
        >[/color]

        I guess an alternative is to set a global flag for each thing once it
        is created, but that seems even more kludgy than just testing if the
        thing exists - if(thing) or if(thingWasCrea ted) - hmmm.

        [...][color=blue][color=green]
        >> 3. For each script that needs to use the array, check the length
        >> right at the start and return (maybe with some error value) if the
        >> length is zero.[/color]
        >
        >
        > That's what I'm doing, although it seems suboptimal.[/color]

        The only other way I can think of is to add intrinsic events
        programmaticall y. If required objects aren't created, don't attach
        the events - but it seems simpler to check for them as required and
        handle their absence gracefully.

        Strange things happen in browsers if users click in the page before
        it has finished loading - silly things can happen even if no JS is
        involved at all.

        You have no control over that, so best just to take a belt 'n braces
        approach, however hackish that may seem.

        Cheers, :-)


        --
        Rob

        Comment

        Working...