Accessing an IFrame's properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron Gray

    Accessing an IFrame's properties

    Hi,

    I want to access the properties of an IFrame but seem unable to get access
    to the IFrames document body.

    <html>

    <body>
    <iframe src="test.html" id="IFrame"></iframe>
    </body>

    <script>
    var f = document.getEle mentById('IFram e');
    f.document.body .innerHTML = "This is another test";
    </script>

    </html>

    This writes to the main document not the IFrame.

    I am doing something very basic wrong but cannot work out what. (I do know I
    should be doing a open/write/close rather than an innerHTML but this is just
    for testing access to the contained IFrames properties)

    Help,

    Aaron


  • Thomas 'PointedEars' Lahn

    #2
    Re: Accessing an IFrame's properties

    Aaron Gray wrote:
    [color=blue]
    > I want to access the properties of an IFrame but seem unable to get access
    > to the IFrames document body.
    >
    > <html>
    >
    > <body>
    > <iframe src="test.html" id="IFrame"></iframe>
    > </body>
    >
    > <script>
    > var f = document.getEle mentById('IFram e');
    > f.document.body .innerHTML = "This is another test";
    > </script>
    >
    > </html>
    >
    > This writes to the main document not the IFrame.[/color]

    Of course.
    [color=blue]
    > I am doing something very basic wrong but cannot work out what.[/color]

    You are obtaining a reference to an HTMLIFrameEleme nt object, representing
    an `iframe' element node in the document tree. By modifying the former's
    proprietary `innerHTML' property, you modify the content of the `iframe'
    element. This content is displayed only if the `iframe' element is not
    supported.
    [color=blue]
    > (I do know I should be doing a open/write/close rather than an innerHTML
    > but this is just for testing access to the contained IFrames properties)[/color]

    You need to access the document object of the IFrame resource instead.

    Proprietary: window.frames['IFrame'].document
    (preferably with `name' instead)
    <URL:http://research.nihons oft.org/javascript/ClientReference JS13/frame.html>
    <URL:http://msdn.microsoft. com/workshop/author/dhtml/reference/objects/obj_window.asp>

    Standards compliant: f.contentDocume nt
    <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-50708718>

    Similar questions have been asked here before.


    PointedEars

    Comment

    • Aaron Gray

      #3
      Re: Accessing an IFrame's properties

      > I want to access the properties of an IFrame but seem unable to get access[color=blue]
      > to the IFrames document body.[/color]

      Properties are accessable but content is not on IE 6, previous versions
      allowed access to the contents.

      Firefox allows access to the contents of an IFrame, but IE6 does not.

      Aaron


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Accessing an IFrame's properties

        Aaron Gray wrote:
        [color=blue][color=green]
        >> I want to access the properties of an IFrame but seem unable to get
        >> access to the IFrames document body.[/color]
        >
        > Properties are accessable but content is not on IE 6, previous versions
        > allowed access to the contents.
        >
        > Firefox allows access to the contents of an IFrame, but IE6 does not.[/color]

        Nonsense.


        PointedEars

        Comment

        • Aaron Gray

          #5
          Re: Accessing an IFrame's properties

          >> Properties are accessable but content is not on IE 6, previous versions[color=blue][color=green]
          >> allowed access to the contents.
          >>
          >> Firefox allows access to the contents of an IFrame, but IE6 does not.[/color]
          >
          > Nonsense.[/color]

          Try it and please send me some code that disputes this !

          I would be most greatful if you can prove me wrong.

          Aaron


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Accessing an IFrame's properties

            Aaron Gray wrote:
            [color=blue][color=green][color=darkred]
            >>> Properties are accessable but content is not on IE 6, previous versions
            >>> allowed access to the contents.
            >>>
            >>> Firefox allows access to the contents of an IFrame, but IE6 does not.[/color]
            >>
            >> Nonsense.[/color]
            >
            > Try it and please send me some code that disputes this !
            >
            > I would be most greatful if you can prove me wrong.[/color]

            <URL:http://pointedears.de/scripts/test/dom/iframe>


            PointedEars

            Comment

            • Aaron Gray

              #7
              Re: Accessing an IFrame's properties

              >>>> Properties are accessable but content is not on IE 6, previous versions[color=blue][color=green][color=darkred]
              >>>> allowed access to the contents.
              >>>>
              >>>> Firefox allows access to the contents of an IFrame, but IE6 does not.
              >>>
              >>> Nonsense.[/color]
              >>
              >> Try it and please send me some code that disputes this !
              >>
              >> I would be most greatful if you can prove me wrong.[/color]
              >
              > <URL:http://pointedears.de/scripts/test/dom/iframe>[/color]

              I seem to be able to read but not modify innerHTML.

              I'll modify my statement :-

              IFrame content is readable but not modifiable from the parent
              window.

              Do you agree ?

              Thurther :-

              <iframe src="document.h tml" name="IFrame" id="IFrame"></iframe>
              <div id="sourceViewD iv"></div>

              document.getEle mentById("sourc eViewDiv").inne rHTML =
              window.frames['IFrame'].document.body. innerHTML;
              window.alert(wi ndow.frames['IFrame'].document.body. innerHTML);

              The first script line fails but the alert works.

              Am very confused,

              Aaron


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Accessing an IFrame's properties

                Please provide attribution of quoted material, as described in
                the Netiquette Guidelines (RFC1855) and the newsgroup's FAQ.[1]
                vvvvvvvvvvvvvvv vv
                Aaron Gray wrote:
                [color=blue][color=green][color=darkred]
                >>>>> Properties are accessable but content is not on IE 6, previous
                >>>>> versions allowed access to the contents.
                >>>>>
                >>>>> Firefox allows access to the contents of an IFrame, but IE6 does not.
                >>>> Nonsense.
                >>> Try it and please send me some code that disputes this !
                >>>
                >>> I would be most greatful if you can prove me wrong.[/color]
                >> <URL:http://pointedears.de/scripts/test/dom/iframe>[/color]
                >
                > I seem to be able to read but not modify innerHTML.[/color]

                I overlooked that part.
                [color=blue]
                > I'll modify my statement :-
                >
                > IFrame content is readable but not modifiable from the parent
                > window.
                >
                > Do you agree ?[/color]

                That would depend on what you call "IFrame content". Try the URL again
                (I modified the document). The first and third button do work for me in
                IE 6.0 SP1 [Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)]. All
                buttons work in Firefox 1.5.0.1 [Mozilla/5.0 (X11; U; Linux i686; en-US;
                rv:1.8.0.1) Gecko/20060313 Debian/1.5.dfsg+1.5.0. 1-4 Firefox/1.5.0.1].
                [color=blue]
                > Thurther :-
                >
                > <iframe src="document.h tml" name="IFrame" id="IFrame"></iframe>
                > <div id="sourceViewD iv"></div>
                >
                > document.getEle mentById("sourc eViewDiv").inne rHTML =
                > window.frames['IFrame'].document.body. innerHTML;[/color]

                It does not appear as if you were attempting to modify the IFrame
                document content here.
                [color=blue]
                > window.alert(wi ndow.frames['IFrame'].document.body. innerHTML);
                >
                > The first script line fails but the alert works.[/color]

                ISTM either you have mixed up the sides of the assignment, or you have
                described the wrong problem. If the latter, you should make sure that
                you are operating on a Valid document first[2], and that
                document.getEle mentById(...) returns an element object reference.[3]
                "The first script line fails" is no error report.[4]


                PointedEars
                ___________
                [1] <URL:http://jibbering.com/faq/>
                [2] <URL:http://validator.w3.or g/>
                [3] <URL:http://pointedears.de/scripts/test/whatami#inferen ce>
                [4] <URL:http://jibbering.com/faq/#FAQ4_43>

                Comment

                • BootNic

                  #9
                  Re: Accessing an IFrame's properties

                  > "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote:[color=blue]
                  > news:8544488.KA NrExqyW2@Pointe dEars.de....
                  > [snip]
                  >
                  > That would depend on what you call "IFrame content". Try the URL
                  > again (I modified the document). The first and third button do
                  > work for me in IE 6.0 SP1 [Mozilla/4.0 (compatible; MSIE 6.0;
                  > Windows NT 5.0)]. All buttons work in Firefox 1.5.0.1 [Mozilla/5.0
                  > (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060313
                  > Debian/1.5.dfsg+1.5.0. 1-4 Firefox/1.5.0.1].
                  > [/color]
                  [snip]
                  How would it work in all them browsers if you replace contentDocument
                  with contentWindow.d ocument?

                  --
                  BootNic Wednesday, April 05, 2006 4:15 PM

                  It's not that some people have willpower and some don't. It's that
                  some people are ready to change and others are not.
                  *James Gordon*

                  Comment

                  • Aaron Gray

                    #10
                    Re: Accessing an IFrame's properties

                    "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                    news:8544488.KA NrExqyW2@Pointe dEars.de...[color=blue]
                    > Please provide attribution of quoted material, as described in
                    > the Netiquette Guidelines (RFC1855) and the newsgroup's FAQ.[1]
                    > vvvvvvvvvvvvvvv vv
                    > Aaron Gray wrote:
                    >[color=green][color=darkred]
                    >>>>>> Properties are accessable but content is not on IE 6, previous
                    >>>>>> versions allowed access to the contents.
                    >>>>>>
                    >>>>>> Firefox allows access to the contents of an IFrame, but IE6 does not.
                    >>>>> Nonsense.
                    >>>> Try it and please send me some code that disputes this !
                    >>>>
                    >>>> I would be most greatful if you can prove me wrong.
                    >>> <URL:http://pointedears.de/scripts/test/dom/iframe>[/color]
                    >>
                    >> I seem to be able to read but not modify innerHTML.[/color]
                    >
                    > I overlooked that part.
                    >[color=green]
                    >> I'll modify my statement :-
                    >>
                    >> IFrame content is readable but not modifiable from the parent
                    >> window.
                    >>
                    >> Do you agree ?[/color]
                    >
                    > That would depend on what you call "IFrame content". Try the URL again
                    > (I modified the document). The first and third button do work for me in
                    > IE 6.0 SP1 [Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)]. All
                    > buttons work in Firefox 1.5.0.1 [Mozilla/5.0 (X11; U; Linux i686; en-US;
                    > rv:1.8.0.1) Gecko/20060313 Debian/1.5.dfsg+1.5.0. 1-4 Firefox/1.5.0.1].
                    >[color=green]
                    >> Thurther :-
                    >>
                    >> <iframe src="document.h tml" name="IFrame" id="IFrame"></iframe>
                    >> <div id="sourceViewD iv"></div>
                    >>
                    >> document.getEle mentById("sourc eViewDiv").inne rHTML =
                    >> window.frames['IFrame'].document.body. innerHTML;[/color]
                    >
                    > It does not appear as if you were attempting to modify the IFrame
                    > document content here.[/color]

                    No just reading its text into a div.
                    [color=blue][color=green]
                    >> window.alert(wi ndow.frames['IFrame'].document.body. innerHTML);
                    >>
                    >> The first script line fails but the alert works.[/color]
                    >
                    > ISTM either you have mixed up the sides of the assignment, or you have
                    > described the wrong problem. If the latter, you should make sure that
                    > you are operating on a Valid document first[2], and that
                    > document.getEle mentById(...) returns an element object reference.[3]
                    > "The first script line fails" is no error report.[4][/color]

                    It was returning null.

                    Your example shows the modification of IFrame innerHTML working.

                    Looks like it is a timing error in my code. Doing mods explicitly to the
                    IFrame on loading the parent document causes a 'null' value for that
                    documents body.

                    Many thanks for your help Thomas,

                    Aaron



                    Comment

                    • Declan Naughton

                      #11
                      Re: Accessing an IFrame's properties

                      Seeing as window.frames isn't a part of any w3c standard, surely there
                      must be another (standards-compliant) way to access the properties of a
                      document included using the iframe or object elements (preferably
                      object)... right?

                      *** Sent via Developersdex http://www.developersdex.com ***

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: Accessing an IFrame's properties

                        Declan Naughton <piratepenguin@ gmail.com> writes:
                        [color=blue]
                        > Seeing as window.frames isn't a part of any w3c standard, surely there
                        > must be another (standards-compliant) way to access the properties of a
                        > document included using the iframe or object elements (preferably
                        > object)... right?[/color]

                        Yes.
                        ---
                        var iframeElem = document.getEle mentById("myIfr ameId");
                        var iframeDoc = iframeElem.cont entDocument;
                        iframeDoc.body. style.backgroun dColor="red"; // i'm in!
                        ---

                        Does it work in IE? Ofcourse not.

                        In IE, iframe elements does not have a "contentDocumen t" property.
                        They do have a "contentWindow" , so you can do:
                        ---
                        var iframeDoc = iframeElem.cont entDocument ||
                        (iframeElem.con tentWindow && iframeElem.cont entWindow.docum ent);
                        ---

                        Good luck.
                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Declan Naughton

                          #13
                          Re: Accessing an IFrame's properties

                          Nice, that works for objects too.

                          Thanks!

                          *** Sent via Developersdex http://www.developersdex.com ***

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: Accessing an IFrame's properties

                            Declan Naughton wrote:
                            [color=blue]
                            > Seeing as window.frames isn't a part of any w3c standard, surely there
                            > must be another (standards-compliant) way to access the properties of a
                            > document included using the iframe or object elements (preferably
                            > object)... right?[/color]

                            Like many things, standards compliance is not an absolute on the Web. In
                            fact, there is no fully standards-compliant approach to the DOM as the W3C
                            DOM standards define only interfaces that can be implemented. However, the
                            implementation always is proprietary; so is also `document' or
                            `window.documen t' to implement _also_ the HTMLDocument interface of W3C DOM
                            Level 2 HTML. One could only argue that

                            document.getEle mentById("myIfr ameId").content Document

                            as Lasse suggested, is "more standards-compliant" than

                            window.frames[myIframeId].contentDocumen t

                            because the proprietary part of the former is "smaller". But I doubt this
                            holds water. However, knowing this, I also tend to call something
                            "standards compliant" if only the implementation is the proprietary part of
                            the reference.

                            Trying to achieve maximum standards compliance can have its drawbacks. For
                            example, usually you want to give your IFrame a name instead of an ID
                            because UAs want the `name' attribute value for the `target' property
                            value of a[href]s. Trying to be standards compliant here would mean

                            document.getEle mentsByName("my IframeName")[0].contentDocumen t

                            (not as this reference worm, of course) as compared to

                            window.frames[myIframeName].contentDocumen t

                            Certainly I would almost always prefer the latter here as first
                            alternative in a collection of dependent feature tests; it has
                            broader support, and is more efficient. Hopefully, the Web API
                            Specification will make that quasi-standard a specified standard
                            eventually.


                            Please quote the minimum of what you are replying to --

                            <URL:http://jibbering.com/faq/faq_notes/pots1.html>
                            <URL:http://www.safalra.com/special/googlegroupsrep ly/>

                            -- and don't use buggy Web interfaces to NetNews such as Developersdex's
                            AspNNTP. Google Groups' interface is slightly better than that (at least
                            it supports threads properly and with a few tweaks also automatic
                            quotation), but a newsreader program is best.


                            PointedEars
                            --
                            #define QUESTION ((bb) || !(bb))
                            // William Shakespeare (if he would have been a hacker ;-))

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: Accessing an IFrame's properties

                              Declan Naughton wrote:
                              [color=blue]
                              > Seeing as window.frames isn't a part of any w3c standard, surely there
                              > must be another (standards-compliant) way to access the properties of a
                              > document included using the iframe or object elements (preferably
                              > object)... right?[/color]

                              Like many things, standards compliance is not an absolute on the Web. In
                              fact, there is no fully standards-compliant approach to the DOM as the W3C
                              DOM standards define only interfaces that can be implemented. However, the
                              implementation always is proprietary; so is also `document' or
                              `window.documen t' to implement _also_ the HTMLDocument interface of W3C DOM
                              Level 2 HTML. One could only argue that

                              document.getEle mentById("myIfr ameId").content Document

                              as Lasse suggested, is "more standards-compliant" than

                              window.frames[myIframeId].contentDocumen t

                              because the proprietary part of the former is "smaller". But I doubt this
                              holds water. However, knowing this, I also tend to call something
                              "standards compliant" if only the implementation is the proprietary part of
                              the reference.

                              Trying to achieve maximum standards compliance can have its drawbacks. For
                              example, usually you want to give your IFrame a name instead of an ID
                              because UAs want the `name' attribute value for the `target' attribute
                              value of a[href]s. Trying to be standards compliant here would mean

                              document.getEle mentsByName("my IframeName")[0].contentDocumen t

                              (not as this reference worm, of course) as compared to

                              window.frames[myIframeName].contentDocumen t

                              Certainly I would almost always prefer the latter here as first
                              alternative in a collection of dependent feature tests; it has
                              broader support, and is more efficient. Hopefully, the Web API
                              Specification will make that quasi-standard a specified standard
                              eventually.


                              Please quote the minimum of what you are replying to --

                              <URL:http://jibbering.com/faq/faq_notes/pots1.html>
                              <URL:http://www.safalra.com/special/googlegroupsrep ly/>

                              -- and don't use buggy Web interfaces to NetNews such as Developersdex's
                              AspNNTP. Google Groups' interface is slightly better than that (at least
                              it supports threads properly and with a few tweaks also automatic
                              quotation), but a newsreader program is best.


                              PointedEars
                              --
                              #define QUESTION ((bb) || !(bb))
                              // William Shakespeare (if he would have been a hacker ;-))

                              Comment

                              Working...