Firefox says the document.frames are undefined

I’m having trouble with a bit of code that works with ie but does not seem to work with firefox. When lookin though the javascript console I discovered that firefox claimed that the document.frames part of the code was undefined. Here is some of the code relevant to the document.frames



document.write("<iframe name='index' src='" + location.href.split("?")[0] + "' style='display:none' onload='harvest()'></iframe>")

function harvest()
{
  var div = document.frames["index"].document.getElementsByTagName("div")
  for(var i = 0; i < div.length; i++)
      if(div[i].className == "thin")
          break
  var a = div[i].getElementsByTagName("A")
  var names = new Array(a.length)
  for(i = 0; i < a.length; i++)
      names[i] = a[i].innerHTML.replace(/<[^>]+>/g, "")
  updateStatus(names)
}


why doesn’t this work in firefox? Why is it undefined?

The use of frames to address an array of iframe objects is IE-only. You can give your single iframe an id, then use getElementById(), or you can create your own array of iframe elements with getElementsByTagName().

I really don’t know what I’m doing any more. My brain isn’t working well and I have been playing with the code and I have realized that I don’t know what I’m doing, this is what I have:

document.write("<iframe id='index' name='index' src='" + location.href.split("?")[0] + "' style='display:none' onload='harvest()'></iframe>")

function harvest()
{
  var div = document.getElementById("index").document.getElementsByTagName("div")
  for(var i = 0; i < div.length; i++)
      if(div[i].className == "thin")
          break
  var a = div[i].getElementsByTagName("A")
  var names = new Array(a.length)
  for(i = 0; i < a.length; i++)
      names[i] = a[i].innerHTML.replace(/<[^>]+>/g, "")
  updateStatus(names)
}

I don’t think I can apply what I have here. I knew it wouldn’t work before I tried it. I don’t know how to do what you have suggested.

At first glance it looks like it might work if you fixed your syntactic errors: You’re missing your curly braces for your for loops; The break command should be on the same line as your if conditional, if that what you’re trying to trigger, or you could use curly braces there; It is really best practice to finish your statements with a semicolon.

… I still can’t figure it out, and I’m doing things I shouldn’t probably.


document.write("<iframe id='index' name='index' src='" + location.href.split("?")[0] + "' style='display:none' onload='harvest()'></iframe>");

function harvest()
{
  var div = document.getElementById("index").getElementsByTagName("div");
  {for(var i = 0; i < div.length; i++)
     if(div[i].className == "thin")break
  var a = div[i].getElementsByTagName("A");
  var names = new Array(a.length);}
  {for(i = 0; i < a.length; i++)
      names[i] = a[i].innerHTML.replace(/<[^>]+>/g, "");}
  updateStatus(names);
}

my javascript console claims

div[i]

has no properties

You can’t access the contents of an iframe like that.

frames is not a collection of document; it’s a collection of window.

window.frames["index"]

window.frames is an array of frames; document.frames, in IE, is an array of iframes.

Kravvitz you are now my best friend… it worked, I also thank you JVLB, for all the time you spent helping me.

You’re welcome. :slight_smile:

You’re right. I forgot about that, sorry.

When iframes are given a name (I’m not sure about id’s) they are added to window.frames as well, which is why this works in this case.

I like using window.frames because it works in IE4 and DOM1 browsers.