| re: Accessing elements drawn with script
Christopher Benson-Manica wrote:[color=blue]
> RobG <rgqld@iinet.net.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
> getElementsByName().[/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(thingWasCreated) - 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
programmatically. 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 |