Connecting Tech Pros Worldwide Help | Site Map

Accessing elements of IFrame..

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: Oct 13 '08
I want to access the elements of IFrame.
Suppose here is my code ....
Expand|Select|Wrap|Line Numbers
  1. <iframe .... ></iframe>
  2.  
Now i want to access the elements inside the IFrame ..how can i do that ?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Oct 13 '08

re: Accessing elements of IFrame..


For standards-compliant browsers, use contentDocument to get access to the document within the iframe. IE requires contentWindow.document.

If you use the window.frames[] syntax, you can just use document.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: Oct 15 '08

re: Accessing elements of IFrame..


Quote:

Originally Posted by acoder

For standards-compliant browsers, use contentDocument to get access to the document within the iframe. IE requires contentWindow.document.

If you use the window.frames[] syntax, you can just use document.

Why JavaScript comes up with two flavors?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Oct 15 '08

re: Accessing elements of IFrame..


No, JavaScript doesn't come with two flavours. Browsers sometimes choose not to implement standards which is where things go wrong and, more often than not, it's IE which is the culprit. There are parts of JavaScript where there are no standards at present. In that case, browsers have to come to some sort of agreement on naming, behaviour, etc.

Edit: did you mean the two different types of syntax?
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#5: Oct 15 '08

re: Accessing elements of IFrame..


Quote:

Originally Posted by acoder

No, JavaScript doesn't come with two flavours. Browsers sometimes choose not to implement standards which is where things go wrong and, more often than not, it's IE which is the culprit. There are parts of JavaScript where there are no standards at present. In that case, browsers have to come to some sort of agreement on naming, behaviour, etc.

Edit: did you mean the two different types of syntax?

Sorry you didn't get me ... ;)
Suppose this is my code ..
Expand|Select|Wrap|Line Numbers
  1. <iframe name='_name' id='_id' ... ></iframe>
  2.  
Now i am accessing the document object of IFrame ...
Expand|Select|Wrap|Line Numbers
  1. var doc = document.getElementById('_id').contentDocument; //Mozilla
  2. var doc = document.getElementById('_id').contentWindow.document //IE
  3. var doc = window.frames['_name'].document; //both
  4.  
Now my question is ..that
document.getElementById('_id') and window.frames['_name'] refers the same object ..i mean the iIFrame window.

For one case contentDocument or contentWindow.document and for another case it is cimply document ..why?
This is my question ?
I think you get my question ... ;)
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Oct 15 '08

re: Accessing elements of IFrame..


This link will explain. It boils down to whether it's a frame or an object. The first syntax is used for an object while the second for a frame.
Reply