Create array on load page

F

Francois

Is it possible to automatically create an array on load page?

I want my script to be external.
(<script language="JavaScript" src="somescript.js"></script>)

Several pages should use this script. I have an unknown number of <DIV>
elements in each page, all with a unique id (ID="name"). I need an array
with all the names of these div-elements in it, the order is unimportant.

Can someone help me out here?
 
L

Laurent Bugnion, GalaSoft

Hi,
Is it possible to automatically create an array on load page?

I want my script to be external.
(<script language="JavaScript" src="somescript.js"></script>)

Several pages should use this script. I have an unknown number of <DIV>
elements in each page, all with a unique id (ID="name"). I need an array
with all the names of these div-elements in it, the order is unimportant.

Can someone help me out here?

You can create an array when the page loads, however this array will be
only loaded in RAM, moreover only in the scope of the window where the
script is executed. If you want that other windows have access to this
array, you must leave the document in the window, untouched, so that
other windows can access it. If you load another document in the window,
the array will be deleted.

You cannot save the array in a file using client-side JavaScript. You
could save it in a cookie, providing you are sure that the client
supports cookie, and that the serialized array will fit in the cookie
(size...).

Does it sound like something responding to your needs?

Laurent
 
L

Lasse Reichstein Nielsen

Francois said:
Is it possible to automatically create an array on load page?

If you want something to happen when a page has finished loading,
you should put it in the body's onload handler.

I want my script to be external.
(<script language="JavaScript" src="somescript.js"></script>)

Put the code below in "somescript.js".
Several pages should use this script. I have an unknown number of <DIV>
elements in each page, all with a unique id (ID="name"). I need an array
with all the names of these div-elements in it, the order is unimportant.

---
var divNames=[];
function doSomething() {
var divs;
if (document.getElementsByTagName) {
divs = document.getElementsByTagName("div"); // Modern browsers
} else {
divs = document.all.tags["div"]; // Old IE browsers
}
for (var i=0;i<divs.length;i++) {
divNames=divs.id;
}
}
 
R

Richard Cornford

divs = document.all.tags["div"]; // Old IE browsers
<snip>

Genuine IE browsers seem very tolerant of the use of - [ ... ] - instead
of - ( ... ) - and vice versa, but - tags - is a method of the - all -
object so a more formally correct syntax would be:-

divs = document.all.tags("div"); // Old IE browsers,
// and unusual IE spoofers.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top