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

Create array on load page

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Francois wrote:
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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #2

P: n/a
"Francois" <francois.sachs@gidesign,nl> writes:
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.

<body onload="doSomething()">
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[i]=divs[i].id;
}
}
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:vf**********@hotpop.com...
<snip>
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.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.