Link to home
Start Free TrialLog in
Avatar of newbee2MFC
newbee2MFC

asked on

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?

Thanks
g
Avatar of VincentPuglia
VincentPuglia

Hi,

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

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


alert(theFrame.document)
var docObj = theFrame.document
alert(docObj)

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

docObj.body
docObj.scripts
etc.

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

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

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

Try this:

document.getElementById("myIFrame").contentDocument

for the IFRAME's document object and

document.getElementById("myIFrame").contentDocument.defaultView

for its window object.

(These work in Mozilla and appear to be W3C-compliant.)
Avatar of newbee2MFC

ASKER

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?
mind posting some html to support what you said?
www.hiveminds.net/jon/test/C_parent_iframe.html -- uses an IFRAME to include an HTML page
www.hiveminds.net/jon/test/C_parent_object.html -- 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);
  }
</script></head>

<body id="parentBody">
.....blahblaha
<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>
.....

Vinny
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.


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

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

alert(MyIFrame);

Result:
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>

alert(MyIFrame);

Result:
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.
2)
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 wonder...how 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

Vinny
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.)

Vinny
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial