473,378 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
3 1581
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Julius Mong | last post by:
Dear all, I have something like this: <html... > <embed ...> </html> Am I out of luck if I wanted to access the embedded DOM and manipulate its content? Or if I have:
3
by: Martien van Wanrooij | last post by:
I am working on a site with some pages that all have a form that starts with a group of radiobuttons. By default none of the buttons is checked. Before submitting the form there is a validation...
17
by: Michael Hill | last post by:
I created an element on-the-fly using javascript like: myA=document.createElement("A"); myA.href="Javascript:acton(this)"; myA.className = "smallLink";...
6
by: Claude Schneegans | last post by:
Hi, In the code below, the lists elements at level 1 have a z-index of 1 and those at level 2 have it set to 2. Normaly, since level 1 and 2 overlap, level 2 elements should be seen above those...
15
by: Christopher Benson-Manica | last post by:
When are named elements written with script accessible to script? <html><head><script type="text/javascript"> function ready() { alert( document.getElementsByName("div").length ); }...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
7
by: rein.petersen | last post by:
Hey All, I was wondering if there were a way for a script to access it's parent tag without having to use the document.all.tags method which doesn't necessarily identify it if there are more...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
3
by: kosmodisk | last post by:
Hi, I'm having problem accessing javascript-created elements from opened window. This occurs only when I'm including another files in opened window, javascript or css. When I comment out...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.