Click to See Complete Forum and Search --> : Javascript variables and frames
spiderbo
04-09-2003, 04:22 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!
gil davis
04-09-2003, 05:54 AM
First, I would like to point out that using document.all limits you to IE.
In your example function, you would have to pass the window object to the function, not just the name of the frame. If you want to just pass the name, then you need to change the function to this:return window.top[framename].document.getElementById(id); which also addresses the IE-only proble previously mentioned.
spiderbo
04-09-2003, 06:14 AM
Many thanks,
My function did actually cover netscape... but I thought I would keep it simple for the post:
Would your solution work for netscape too? What would the syntax be?
function getLayer(id,framename) {
if (browser == "IE") {
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];
} else if (browser == "NN4") {
if (framename == "top.index_main")
return top.index_main.document.layers[id];
if (framename == "top.index_nav")
return top.index_nav.document.layers[id];
if (framename == "top.index_main.main")
return top.index_main.main.document.layers[id];
if (framename == "top.index_main.news_main")
return top.index_main.news_main.document.layers[id];
} else {
if (framename == "top.index_main")
return top.index_main.document.getElementById(id);
if (framename == "top.index_nav")
return top.index_nav.document.getElementById(id);
if (framename == "top.index_main.main")
return top.index_main.main.document.getElementById(id);
if (framename == "top.index_main.news_main")
return top.index_main.news_main.document.getElementById(id);
}
}
gil davis
04-09-2003, 06:38 AM
My suggestion would work for W3C DOM version browsers (IE5+, NS6+, Mozilla). You would still need to use document.all for IE4 and document.layers for NS4.
spiderbo
04-09-2003, 08:49 AM
I tried the following and it did not work, any ideas? (IE Only)
currentframe = "top.index_main";
function display_cal(object) {
var framename = currentframe.replace(/top./gi,"");
if (window.top[framename].document.layers && window.top[framename].document.layers[object] != null) {
window.top[framename].document.layers[object].visibility = 'visible';
} else {
display_externalcal();
}
}
It keeps running the display_externalcal() function.
gil davis
04-09-2003, 09:17 AM
Originally posted by spiderbo
(IE Only)
... window.top[framename].document.layers ...
} else {
display_externalcal();
}
}
It keeps running the display_externalcal() function.That is exactly what I would expect. In IE, document.layers would always be undefined.
What did you want it to do?
spiderbo
04-09-2003, 10:05 AM
I want to set
window.top[framename].document.layers[object].visibility = 'visible';
if document.layers[object] exits.
If the div does not exist on the specified frame, I want to call the display_externalcal() function.
However, even though the div (layer) does exist, it still keeps calling display_externalcal().
Therefore
if (window.top[framename].document.layers && window.top[framename].document.layers[object] != null)
is not working...
gil davis
04-09-2003, 10:12 AM
(shuffling noises as he gets back up off the floor, tears streaming from his eyes)
What part of In IE, document.layers would always be undefined.are you having trouble with?
You stated (IE Only)Therefore, the layer (and the whole document.layers array) doesn't exist.
spiderbo
04-09-2003, 10:29 AM
lol, I am so sorry, such an idiot!
this is what I meant:
currentframe = "top.index_main";
function display_cal(object) {
var framename = currentframe.replace(/top./gi,"");
if (window.top[framename].document.all[object] != null) {
window.top[framename].document.all[object].visibility = 'visible';
} else {
display_externalcal();
}
}
That doesn't work.
Nor does this:
function display_cal(object) {
var framename = currentframe.replace(/top./gi,"");
if (window.top[framename].document.getElementById(object) != null) {
window.top[framename].document.getElementById(object).visibility = 'visible';
} else {
display_externalcal();
}
}
spiderbo
04-10-2003, 04:19 AM
right, spotted what my problem was, i left out the 'style' in 'style.visibility': :rolleyes:
top.frames[framename].document.all[object].style.visibility = 'visible';
The only problem is that your solution does not work for sub frames.
If framename is index_main.news, it would have to look like:
top.frames["index_main"].frames[framename].document.all[object].style.visibility = 'visible';
So I am back to square one. :(