get document of IFRAME in Safari

I understand it is possible to get document object of a IFRAME in IE via document.get contentWindow.getElementById("IFRAMENAME".document,

Is this possible in Safari bowser?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


you should be able to get the document object simply by referring to it:

<iframe name='theFrame' src='xx.html'></iframe>

var docObj = theFrame.document

if you want to access any of its contents, you simply continue using the DOM:


The only thing you have to be careful of is with 'document.write'
if you want to write to the frame, use

<iframe id="myIFrame" src="sourcefile.html"></iframe>

(Add a name attribute for targeting if you need it.)

Try this:


for the IFRAME's document object and


for its window object.

(These work in Mozilla and appear to be W3C-compliant.)
newbee2MFCAuthor Commented:
var iFrame = theDoc.getElementById(id) ;
alert(  iFrame );  //----> display as IFRAME in runtime

alert(iFrame.document); //-> display as undefined
alert(iFrame.contentDocument); //-> display as undefined as well

the code works perfect fine in Mozilla, IE and Netscape
Any idea?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mind posting some html to support what you said?
ZontarCommented: -- uses an IFRAME to include an HTML page -- uses an OBJECT to include an HTML page

Both examples work in Mozilla and Opera (but not in MSIE).

I'm not sure if this is what you are looking for, but it works for me (ie6.0)
 function moveObjectToAnchor()
   var objectAnchor = myObject.document.getElementsByTagName('a');

   for (var i = 0; i < objectAnchor.length; i++)
     if (objectAnchor[i].name == 'myAnchor')
         objectAnchor = objectAnchor[i]
   myObject.scrollTo(0, objectAnchor.offsetTop);

<body id="parentBody">
<p><a href="javascript:moveObjectToAnchor();">Make the OBJECT scroll</a>.</p>
<p>(reload page to try again)</p>
<p>blah blah blah</p>
<iframe id="myObject" width="250" height="250" type="text/html" name='myObject' src="C_parent_object_files/C_iframe.html"></iframe>

Using something like myIFrame in place of document.getElementById("myIFrame") is nonstandard and works on MSIE only. It's not a good habit to be in if you want scripts to work cross-browser. document.scripts is also IE-only. If you stick with W3C DOM your scripts will work in all of the version 5+ browsers about 80-90% of the time.

>  the code works perfect fine in Mozilla, IE and Netscape

Which code did you mean?

> alert(iFrame.document); //-> display as undefined
> alert(iFrame.contentDocument); //-> display as undefined as well

This was in Safari? Please be specific. Thanks.
Hi Zontar

  Which is non-standard -- Netscape 6.1 or Mozilla Firebird or IE6 ?  They all were tested with the code posted above.  

  So, I guess your above statement is not only ill-advised, but wrong.

Hold on a minute, I think we've got a disconnect happening here. Let's review:

<iframe id="myIFrame" src="myfile.html"></iframe>


Mozilla 1.5 -- Error: MyIFrame is not defined.
MSIE 6.0 -- displays: [object]
Opera 7.2 -- displays: [objectObject]

So yes this will get a reference to the iframe in MSIE and Opera, but -- as I've been saying -- it doesn't work in Mozilla, and it's not per the DOM specs.

<iframe name="myIFrame" src="myfile.html"></iframe>


Mozilla 1.5 -- displays: [object Window]
MSIE 6.0 -- displays: [object]
Opera 7.2 -- displays: [objectObject]

Yes, this gets a reference to the iframe in all three browsers. I never said it didn't.

But it's still a nonstandard shortcut for document.frames["myIFrame"]. It causes an overabundance of and over-reliance on global variables. It's not in the W3C DOM spec. It's not supported in XML. It is definitely not supported in serverside DOM. It will definitely not be supported when XML/XHTML-compliant browsers are the norm -- which may happen sooner than you think.

Another issue is that the name attribute will be phased out in future versions of HTML/XHTML. So will frames (including iframes).

Make fun of me if you want, but I'm just trying to stay ahead of the curve.
Staying ahead of the curve is well and fine -- as long as you remember you are still part of the curve and that the rest of the world is concerned about realities.

1) Again, I tested with Mozilla Firebird, NN6.1, IE6.0 -- I make no sweeping claims on things I have not tested with.
3) At this present moment in time, the code I posted above is standard, irregardless of what you may believe
4) There are X million users on the internet; of those, Y% use M$-compatible browsers, Z% are connected through providers such as AOL, MSN, EarthLink -- now I many of those millions of people do you believe will ever get a chance to use XML/ serverside DOM/ XHTML / etc. /etc. ?  My guess: 1% and I am being generous.
5) there is a difference between 'standards and recommendations' and the real world.  The code in the page you posted will not execute for IE.  I guess you are proposing that people should upgrade their IEs for a non-existent standards-compliant browser?
   "Non-existent" because it is the nature of the beast to be a moving target.  By the time a browser reaches compliance, standards will change -- and don't tell me there is a browser out there right now that is compliant because there is none; they all fall short to some degree.

  So, I guess you are telling newbee that the code I posted shouldn't be used because -- while it works in probably every browser -- it won't be supported ten/twenty years down the road.

  I don't have to make fun of you; you are doing a good enough job of it yourself

newbee2MFCAuthor Commented:
ok, guys... I figured out this myself....

my IFRAME definition was using "src=../myfolder/blank.gif".  It worked well after I changed to "src=../myfolder/blank.html".

The code worked fine in IE, Netscape and Mozilla...  but Safari.

I am wondering it this is a Safari bug or non-standard issue?
Hi newbee,

 Not having Safari, I can't say definitively.  Check with the Safari support forum (they should have one, or at least a link to one on their main site.)

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next four days.

EE Cleanup Volunteer
PAQed, with points refunded (20)

E-E Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.