By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

Accessing elements drawn with script

P: n/a
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)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Christopher Benson-Manica wrote:
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?

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.length; i<j; i++){
x = document.getElementsByName(arguments[i]);
k = x.length;
while (k--) { eleArray.push(x[k]); }
}
}

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

--
Rob
Jul 23 '05 #2

P: n/a
RobG <rg***@iinet.net.auau> spoke thus:
How about creating an initArray() function and running it with
the body onload event?
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().
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.
Well, I guess it isn't a hack if no better way 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.
See my first paragraph above.
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.


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)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #3

P: n/a
Christopher Benson-Manica wrote:
RobG <rg***@iinet.net.auau> spoke thus:

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

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().


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...

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.

Well, I guess it isn't a hack if no better way exists...


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.

[...]
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.

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


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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.