PDA

View Full Version : Passing div and layer names into a function, how??


Jeepers
06-29-2002, 04:08 PM
I have a function that writes to a div, layer or ilayer depending upon the browser. I need the same function to write to a number of divs etc all with different names (id). I pass the div etc names to the function as a strings, how do I get that string so that I can use it as the actual name.:confused:

The code they will be used in is: (myStr is the bit that will be written)

document.ilayername.document.layername.document.write(myStr);

and

divname.innerHTML=myStr;

or any other way I can pass the div etc names as parameters.

Thanks

jkd
06-29-2002, 05:13 PM
document['ilayername'].document['layername'].document.write()

And

document.all['divnameorid']

And finally the DOM way:

document.getElementById('divid')

:) You just need to pass a single string argument this way.

adios
06-29-2002, 05:45 PM
function getLayer(id) {
return typeof document.all != 'undefined' ? document.all(id) :
typeof document.getElementById != 'undefined' ? document.getElementById(id) :
typeof document.layers != 'undefined' ? document['NS' + id].document.layers[0] : null;
}

function write_to_layer(id,.....) {
var l = getLayer(id);
if (l) {
.....etc.

I'm guessing you're nesting the layers in NS4 for positioning purposes (table?); if so, just id the container element (<td>, <div>, <span>) and give the enclosed <ilayer> the same id with 'NS' prepended to it. I've always wanted to use 'prepended' in a forum.

You 'insert' pieces of object references in JS by using associative array (rather than dot) notation; so...

document.form_name.element_name

...is more easily modified as

document[form_name][element_name]

...by simply replacing the string 'key' in the brackets.

jkd
06-29-2002, 06:16 PM
I like to think of the brackets as property accessors. This goes for more than just properties though:

15.0['toString']().constructor == String //true

For example. :)

Jeepers
06-29-2002, 07:55 PM
Thanks guys, stunning. It now works in IE6, NS6, NS4.7 and Mozilla, the only one that throwing it's toys out is Opera, but having said that HV Menu, which I use for navigation, does not work in Opera either, sso no big loss.
Thanks again.