...

View Full Version : Using JavaScript Variables



Bodmins
04-09-2003, 11:20 AM
Hi,

I am having problems using/setting a javascript variable.


Consider the following function that I am using:

function getLayer(id,framename) {
if (framename == "top.index_main.main")
return top.index_main.main.document.all[id];
if (framename == "top.index_main.news_main")
return top.index_main.news_main.document.all[id];
if (framename == "top.index_nav")
return top.index_nav.document.all[id];
if (framename == "top.index_main")
return top.index_main.document.all[id];
}

However, if I try to use the following, which would seem to me to be the obvious way to do it, I get errors:

function getLayer(id,framename) {
return framename.document.all[id];
}

My list of frame names is growing, and I do not want to have to use the first method. Is there something that I am doing wrong in my second method to explain why it wont work?

Any suggestions apreciated!

cheesebagpipe
04-09-2003, 09:52 PM
if (framename == "top.index_main.main") //framename is string
return top.index_main.main.document.all[id]; //now it's not

JS code like top.index_main.main is not a string; dot syntax consists of dots (d'oh :p) and identifiers (tokens/variable names). Can't simply 'load a string' into one...you need to use eval() - ugh - or reference with associative array notation, which allows easy mixing of strings and dot syntax.

function getLayer(id,framename) {
return frames[framename].document.all[id];
}

http://www.forerunners.org/WebLibrary/jscript/ch07_06.htm

Shouldn't be using IE-only referencing, but that's another thread...

Bodmins
04-10-2003, 08:48 AM
thank you cheesebagpipe.

I tried something similar and it still didn't work:

framename = "top.index_main";

if (frames[framename].document.all[object] != null) {
frames[framename].document.all[object].visibility='visiblity';
}

I receive the error " 'frames[...].document' is null or not an object ".

Any ideas?

PS. my function is cross browser, I have just put in the IE section to keep it simple.

glenngv
04-10-2003, 03:02 PM
try:

top.frames[framename].document.all[object]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum