2

In the following code, the alert works fine and prints "DIV : IFRAME" as it should however it then says that cNs[1].childNodes[1].document has no properties.

Html:

<div id="WinContainer"> <div style="display: none;"><iframe id="frame1" name="frame1"></iframe></div> <div style="display: none;"><iframe id="frame2" name="frame2"></iframe></div> </div> 

JavaScript:

var cNs = document.getElementById('WinContainer').childNodes; alert(cNs[1].tagName + ' : ' + cNs[1].childNodes[1].tagName); cNs[1].childNodes[1].document.location = 'someurl.pl'; 

BUT if I do this:

frame1.document.location = 'someurl.pl'; 

it works fine.

3
  • 1
    Your code actually doesn't alert "DIV : IFRAME". See jsbin.com/owofo/edit cNs[1].childNodes[1] is null. Commented Dec 8, 2009 at 15:46
  • IE and firefox are indexing them differently Commented Dec 8, 2009 at 15:54
  • @unknown: not with the markup in the question. cNs[1].childNodes[1] is borked in all browsers, since you don't have whitespace anywhere between tags. Commented Dec 8, 2009 at 16:00

4 Answers 4

6

The iframe DOM node has a property called contentDocument which will be the equivalent of document, but for that iframe.

If the page being displayed is on another server tho (or even on a different port on the same server) you will get a security exception trying to access it.

Not sure if this works for IE.

Sign up to request clarification or add additional context in comments.

1 Comment

frame1.document.location works because you are changing the frame's location without being able to read anything from it. For example, you could have a large iframe which loaded either Facebook or Myspace when you clicked a button, but the javascript may not have access to the contents of either, because it could steal your personal data.
1

You can also reference the reliable contentWindow property to get to that iFrame's document object as such:

cNs[0].childNodes[0].contentWindow.document 

Comments

0

I'm not all that familiar with javascript but it looks like you're using cNs incorrectly. It looks as though it is an array containing the child nodes and the code

cNs[1].childNodes[1].document.location = 'someurl.pl'; 

is trying to get the child nodes that do not exist. Try this it might work.

cNs[1].document.location = 'someurl.pl'; 

Comments

0

EDIT: frame1 should be:

cNs[0].childNodes[0].document.location

Remember that things are 0 indexed.

2 Comments

in IE this works, in firefox my elements are indexed at 1,1 not 0,0
Check out quirksmode.org/js/detect.html for a quick tutorial on browser detection if things are being indexed differently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.