Solved

get document of IFRAME in Safari

Posted on 2003-10-30
15
1,280 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:newbee2MFC
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9651830
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(...)

0
 
LVL 11

Expert Comment

by:Zontar
ID: 9655634
<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.)
0
 

Author Comment

by:newbee2MFC
ID: 9659998
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?
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9660132
mind posting some html to support what you said?
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9660684
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).

0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9661026
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
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9662247
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?



0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Expert Comment

by:Zontar
ID: 9662248
> alert(iFrame.document); //-> display as undefined
> alert(iFrame.contentDocument); //-> display as undefined as well

This was in Safari? Please be specific. Thanks.
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9662766
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
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9662964
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.
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9663677
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
0
 

Author Comment

by:newbee2MFC
ID: 9671689
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?
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9672409
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
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10346349
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
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10377144
PAQed, with points refunded (20)

Computer101
E-E Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now