View Full Version : Help needed with NS4 accessor
Algorithm
11-17-2002, 04:26 AM
I wrote the following function with the intent of getting an equivalent in NS4 to the getElementByid function, but for some reason am not getting useful results. Any help would be appreciated.
function getNs4Layer(id, context){
if(!context) context = document;
if(!context.layers) return null;
if(context.layers[id]) return context.layers[id];
var i;
var d = null;
for(i=0; i<context.layers.length; i++){
d = getNs4Layer(id,context.layers[i].document);
if(d!=null) return d;
}
return d;
}
RadarBob
11-17-2002, 05:50 AM
You have FOUR returns in one small function - ouch! That might be a record. You're asking for real hairpulling testing and maintaining that knot of code. I'm not sure what's going on in there but here's a suggestion:
Initialize a variable to null, then loop through your layer(s) w/out trying to "short-curcuit" it for special cases. A compound IF should help alot. (i.e. if( this || that) ). If you find the right id, set it to the varible. At the end, pass back the variable. You should have only one "return" in the whole function if you do it right.
You're making it way too convoluted by trying to make sure "d" is never null. Just put whatever you find in "d". What diff does it make if it's null?
If what's returned is null that's cool. Just let your calling code decide what to do. You'd use the new function like this:
if (getNs4Layer(id, context) == null) {
alert ("didn't find it");
}else{
// do whatever
}
OR
var theThing = null;
theThing = getNs4Layer(id, context);
if (theThing == null) {
alert ("didn't find it");
}else{
// do whatever
}
Algorithm
11-17-2002, 08:15 PM
Following your suggestion, I have rewritten the function; however, the problem persists. I am told that the return value has no properties.
I'm using NS4.8 on Windows XP to test.
function getNs4Layer(id, context){
var i;
var d = null;
if(!context) context = document;
if(context.layers){
if(context.layers[id]) d = context.layers[id];
for(i=0; (d==null && i<context.layers.length); i++){
d = getNs4Layer(id,context.layers[i].document);
}
}
return d;
}
jolietjake
11-18-2002, 10:45 AM
Try this function, I think it works for NS4 and IE
function objHtml(n, d) {
var p,i,x;
if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
}
if (d.getElementById)
x = d.getElementById(n);
if(!x && !(x=d[n]) && d.all)
x=d.all[n];
for (i=0; !x && i<d.forms.length; i++)
x=d.forms[i][n];
for(i=0; !x && d.layers &&i< d.layers.length; i++)
x=objHtml(n,d.layers[i].document);
return x;
}
Algorithm
11-18-2002, 09:44 PM
From what I can see in that function, the only real difference between your implementation and mine (when dealing with NS4) is that yours accesses document[id] instead of document.layers[id]. I made the corresponding change in my function, with no results.
On the offchance that I missed something, I also pasted your version into my code verbatim and got the same error: the return value has no properties.
And yes, I'm waiting for the page to load before calling this function.
Can anyone see the problem here?
jolietjake
11-19-2002, 08:04 AM
Well, perhaps the error isn't in the function. Is long time since my last html in NS4, but this function was working well in the past...
glenngv
11-19-2002, 09:04 AM
your code works just fine for me in NS4.78 on Windows.
<html>
<head>
<script language="javascript">
function getNs4Layer(id, context){
var i;
var d = null;
if(!context) context = document;
if(context.layers){
if(context.layers[id]) d = context.layers[id];
for(i=0; (d==null && i<context.layers.length); i++){
d = getNs4Layer(id,context.layers[i].document);
}
}
return d;
}
</script>
</head>
<body onload="getNs4Layer('ns4',document).visibility='show'">
<layer id="ns4" visibility="hide">This is initially a hidden layer</layer>
</body>
</html>
Maybe the error is in how you call the function and use the returned object or maybe in HTML.
Algorithm
11-19-2002, 09:26 AM
[Badly edited, explanation below]
glenngv
11-19-2002, 09:39 AM
you didn't notice it, i set the visibility from 'hide' to 'show' :p
Algorithm
11-19-2002, 09:42 AM
Sorry, I didn't check that until after changing the tag to a div... :o
I was trying to change my previous post back when you replied and lost my first edit, so unfortunately no-one else will have any idea what I'm talking about, so let's move on!
Here's the original content of my previous post, which now applies again :rolleyes::
Well, I figured out the problem, but I don't know how to get around it. I tried your code and it worked. I then changed the <layer> to a <div> (which is what I've been using) and got an error. Is there any way around this, or will I have to dynamically load my content every time?
glenngv
11-19-2002, 09:55 AM
you should use <layer> instead of <div> in NS4, to get consistent result.
take note that layer style is different with divs.
<layer id="nsLayer" width="300" height="55" bgColor="lightyellow" visibility="hide"></layer>
Algorithm
11-19-2002, 10:00 AM
The whole point of this function is to get a cross-browser page to work well in NS4. I've successfully accessed <div> tags in NS4 before, so I know it's possible, and I'd really prefer to avoid using <layer> tags.
So basically, does anyone know how to get the function I have to work with <div> tags?
glenngv
11-19-2002, 10:08 AM
enclose the div tags inside the <ilayer> tag:
<html>
<head>
<script language="javascript">
function getNs4Layer(id, context){
var i;
var d = null;
if(!context) context = document;
if(context.layers){
if(context.layers[id]) d = context.layers[id];
for(i=0; (d==null && i<context.layers.length); i++){
d = getNs4Layer(id,context.layers[i].document);
}
}
return d;
}
</script>
</head>
<body onload="getNs4Layer('ns4',document).visibility='show'">
<ilayer id="ns4" visibility="hide"><div id="IEorNS6">This is initially a hidden layer</div></ilayer>
</body>
</html>
realisis
11-19-2002, 11:08 AM
"the return value has no properties. "
Algorithm, that would be the case if you didn't set the *position* property for the DIV you're trying to access: NS4 will still see the DIV's ID, but if no positioning has been declared, it's not considered a layer.
My apologies if this post is on the wrong track. But I did not see any reference to positioning in the thread, so am wondering if that might be the cause.
glenngv
11-19-2002, 11:47 AM
realisis is correct
try this page and see all the properties of the div in NS4
<html>
<head>
<script language="javascript">
function getNs4Layer(id, context){
var i;
var d = null;
if(!context) context = document;
if(context.layers){
if(context.layers[id]) d = context.layers[id];
for(i=0; (d==null && i<context.layers.length); i++){
d = getNs4Layer(id,context.layers[i].document);
}
}
return d;
}
function mydump(){
props="";
obj = (document.layers)?getNs4Layer('myDiv',document):document.getElementById("myDiv");
for (var i in obj) props+=i + ": " + obj[i] + "<br>"
if (document.layers){
obj.document.write(props);
obj.document.close();
//obj.bgColor="yellow"; //you would have to uncomment this to bring back the background color
}
else obj.innerHTML=props;
}
</script>
</head>
<body onload="mydump">
<div id="myDiv" style="position:absolute;visibility:visible;background-color:yellow;width:300px;height:40px">This is a div/layer</div>
</body>
</html>
but I think, it's too difficult to maintain all the attributes in a single tag. I'm still inclined to use the extra <ilayer> tag for NS4
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.