Accesing an IFrame from an internal element

Discussion in 'Javascript' started by Fernando Deutsch, Feb 22, 2006.

  1. I have an html page with an iframe on it. Inside the iframe there is an img
    element. I am looking how to reference the iframe from the img element
    contained on it and cannot find a way to do it. I am using FireFox 1.5.0.1

    I am looking for something like myImg.container.

    Can someone help me ?

    Thanks in advance


    *** Free account sponsored by SecureIX.com ***
    *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
    Fernando Deutsch, Feb 22, 2006
    #1
    1. Advertising

  2. Fernando Deutsch

    d Guest

    "Fernando Deutsch" <> wrote in message
    news:43fc972a$0$25682$...
    >I have an html page with an iframe on it. Inside the iframe there is an img
    > element. I am looking how to reference the iframe from the img element
    > contained on it and cannot find a way to do it. I am using FireFox 1.5.0.1
    >
    > I am looking for something like myImg.container.
    >
    > Can someone help me ?


    window, isn't it?

    > Thanks in advance
    >
    >
    > *** Free account sponsored by SecureIX.com ***
    > *** Encrypt your Internet usage with a free VPN account from
    > http://www.SecureIX.com ***
    d, Feb 22, 2006
    #2
    1. Advertising

  3. Fernando Deutsch

    Evertjan. Guest

    Fernando Deutsch wrote on 22 feb 2006 in comp.lang.javascript:

    > I have an html page with an iframe on it. Inside the iframe there is
    > an img element. I am looking how to reference the iframe from the img
    > element contained on it and cannot find a way to do it.


    window

    [To the image the page of the iframe is just that,
    perhaps I read your Q wrong, however]

    > I am using FireFox 1.5.0.1


    Are you only building for yourself?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 22, 2006
    #3
  4. No, window returns undefined.

    I have tried so far

    element.window;
    element.ownerDocument.window;

    where element is the img object and both expressions returns undefined.

    Thanks anyway

    "d" <> wrote in message
    news:zK0Lf.24779$...
    > "Fernando Deutsch" <> wrote in message
    > news:43fc972a$0$25682$...
    > >I have an html page with an iframe on it. Inside the iframe there is an

    img
    > > element. I am looking how to reference the iframe from the img element
    > > contained on it and cannot find a way to do it. I am using FireFox

    1.5.0.1
    > >
    > > I am looking for something like myImg.container.
    > >
    > > Can someone help me ?

    >
    > window, isn't it?
    >
    > > Thanks in advance
    > >
    > >
    > > *** Free account sponsored by SecureIX.com ***
    > > *** Encrypt your Internet usage with a free VPN account from
    > > http://www.SecureIX.com ***

    >
    >



    *** Free account sponsored by SecureIX.com ***
    *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
    Fernando Deutsch, Feb 22, 2006
    #4
  5. No, window returns undefined.

    I have tried so far

    element.window;
    element.ownerDocument.window;

    where element is the img object and both expressions returns undefined.

    > Are you only building for yourself?


    I did not understand what you mean.

    Thanks anyway

    "Evertjan." <> wrote in message
    news:Xns9772B7017B35Beejj99@194.109.133.242...
    > Fernando Deutsch wrote on 22 feb 2006 in comp.lang.javascript:
    >
    > > I have an html page with an iframe on it. Inside the iframe there is
    > > an img element. I am looking how to reference the iframe from the img
    > > element contained on it and cannot find a way to do it.

    >
    > window
    >
    > [To the image the page of the iframe is just that,
    > perhaps I read your Q wrong, however]
    >
    > > I am using FireFox 1.5.0.1

    >
    > Are you only building for yourself?
    >
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)



    *** Free account sponsored by SecureIX.com ***
    *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
    Fernando Deutsch, Feb 22, 2006
    #5
  6. Fernando Deutsch

    Jonas Raoni Guest

    Fernando Deutsch wrote:
    > I have tried so far
    >
    > element.window;
    > element.ownerDocument.window;


    This looks quite strange, he asked you to use the window global variable.

    alert(window);

    If you're "inside" the iframe, this will be a reference to it.


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
    Jonas Raoni, Feb 22, 2006
    #6
  7. Fernando Deutsch

    Evertjan. Guest

    Fernando Deutsch wrote on 22 feb 2006 in comp.lang.javascript:

    > "Evertjan." <> wrote in message
    > news:Xns9772B7017B35Beejj99@194.109.133.242...
    >> Fernando Deutsch wrote on 22 feb 2006 in comp.lang.javascript:
    >>
    >> > I have an html page with an iframe on it. Inside the iframe there
    >> > is an img element. I am looking how to reference the iframe from
    >> > the img element contained on it and cannot find a way to do it.

    >>
    >> window
    >>
    >> [To the image the page of the iframe is just that,
    >> perhaps I read your Q wrong, however]


    [please do not toppost on usenet]

    > No, window returns undefined.


    Imposssible, as windows is the top-of-DOM.

    > I have tried so far
    >
    > element.window;
    > element.ownerDocument.window;
    > where element is the img object and both expressions returns
    > undefined.


    This is nonsense code, you cannot go above the window in a page.
    Windows does not have to derive from your img object, as you are on theat
    page with your javascript, acording to your Q.

    Try:
    alert(windows.document.body.style.color)

    Reread your Q and see that the answer to your Q is sound,
    then refrase your question to what you realy wanted to know.

    ===========================

    You left out this quote:
    ">>> I am using FireFox 1.5.0.1 "

    >> Are you only building for yourself?

    >
    > I did not understand what you mean.


    It does not matter if you are using FF or IE,
    it matters what your users use.
    Or is the page build for your personal use only?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 22, 2006
    #7
  8. May look as nonsense code to you, because you dont know the whole context of
    the application.

    Basically I have a html page with the iframe that contains an img element.
    On that page (not inside the iframe) I have a collection that contains a
    reference to the img object of the iframe.

    So I am looking to see what object would be the "window container" of that
    img . As I have a reference to the img object I can navigate to its owner
    document, and from it, I should be able to reach its window (expecting the
    iframe) but I got an undefined object.

    According to the Mozilla Document Interface
    http://www.mozilla.org/docs/dom/domref/dom_doc_ref.html#998664 I should be
    able to get the window by the property contentWindow.

    About the browser, I have resolved the situation for IE, I am looking for a
    solution on FF.

    >
    > This is nonsense code, you cannot go above the window in a page.
    > Windows does not have to derive from your img object, as you are on theat
    > page with your javascript, acording to your Q.
    >
    > Try:
    > alert(windows.document.body.style.color)
    >
    > Reread your Q and see that the answer to your Q is sound,
    > then refrase your question to what you realy wanted to know.
    >
    > ===========================
    >
    > You left out this quote:
    > ">>> I am using FireFox 1.5.0.1 "
    >
    > >> Are you only building for yourself?

    > >
    > > I did not understand what you mean.

    >
    > It does not matter if you are using FF or IE,
    > it matters what your users use.
    > Or is the page build for your personal use only?
    >



    *** Free account sponsored by SecureIX.com ***
    *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
    Fernando Deutsch, Feb 22, 2006
    #8
  9. "Fernando Deutsch" <> writes:

    Please don't top post!

    > May look as nonsense code to you, because you dont know the whole context of
    > the application.


    More likely because he knows how the DOM works.

    You wrote, e.g.:
    element.window
    and
    element.ownerDocument.window

    That's just blind guessing on your part, and it works as well as
    could be expected from that. The element does have an "ownerDocument"
    property (as specified by W3C DOM Core), but neither the document
    nor the element has a reference to a window object.

    > So I am looking to see what object would be the "window container" of that
    > img . As I have a reference to the img object I can navigate to its owner
    > document, and from it, I should be able to reach its window (expecting the
    > iframe) but I got an undefined object.


    The error is your assumption that you can go from a DOM element to
    the window containing the DOM document of the element. The window object
    is not a DOM object, so the DOM specification doesn't say how to find
    it.

    You might be able to use document.defaultView, as some browsers have
    that property point to the window object. It is not specified that the
    defaultView should also be the global object, or a window object, and
    it's not available in all browsers, so it's not a good solution.

    > According to the Mozilla Document Interface
    > http://www.mozilla.org/docs/dom/domref/dom_doc_ref.html#998664 I should be
    > able to get the window by the property contentWindow.


    That's a property on the DOM element of the iframe in the parent document,
    not on an element in the window you want to find.

    You could also just do (in the parent window):
    frames["iframeId"]
    to get the window object of the embedded iframe with id="iframeId".

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Feb 23, 2006
    #9
  10. Fernando Deutsch

    Csaba Gabor Guest

    Lasse Reichstein Nielsen wrote:
    > The error is your assumption that you can go from a DOM element to
    > the window containing the DOM document of the element. The window object
    > is not a DOM object, so the DOM specification doesn't say how to find
    > it.


    Rather unfortunate, that.

    > You might be able to use document.defaultView, as some browsers have
    > that property point to the window object. It is not specified that the
    > defaultView should also be the global object, or a window object, and
    > it's not available in all browsers, so it's not a good solution.


    Thanks for the tip, I had missed it before. I just tested it out on my
    Firefox 1.5 / Win XP Pro and it is working fine. Here is what I did:
    Main page:
    <html><head><title>Containing Page</title>
    <script type='text/javascript'>
    function onLoad() {
    var iframe = document.getElementById('myframe');
    var elemInFrame = iframe.contentDocument.getElementById('myElem');
    var elemWindow = elemInFrame.ownerDocument.defaultView;
    elemWindow.setTimeout ("document.getElementById('myspan').innerHTML =
    \
    'The answer is: mozilla'", 100);
    }
    </script>
    </head><body onload="onLoad()">
    This page contains a single iframe with
    an image element to see if we can get to
    that element's containing window<br><br>
    <iframe id=myframe src="frame.htm"
    style="height:4in;width:6in"></iframe>
    </body></html>


    Frame.htm:
    <html><head><title>Frame Page</title></head>
    <body>
    This is the frame<br><br>
    <span id=myspan style="border:1px solid green">
    This text in the frame should get replaced</span><br><br>
    <div>
    <img id=myElem src="https://www.mozilla.org/images/header_logo.gif">
    </div>
    </body></html>


    The above test also works for IE 6, if I change the middle two lines of
    script to:
    var elemInFrame = iframe.Document.getElementById('myElem');
    var elemWindow = elemInFrame.document.parentWindow;


    Csaba Gabor from Vienna
    Csaba Gabor, Feb 23, 2006
    #10
  11. Csaba Gabor wrote:
    > Lasse Reichstein Nielsen wrote:

    <snip>
    >> You might be able to use document.defaultView, as some browsers
    >> have that property point to the window object. It is not
    >> specified that the defaultView should also be the global object,
    >> or a window object, and it's not available in all browsers, so
    >> it's not a good solution.

    >
    > Thanks for the tip, I had missed it before. I just tested it out
    > on my Firefox 1.5 / Win XP Pro and it is working fine. ...


    And if you tried it out on IceBrowser it would fail reliably.
    IceBrowser's - document.defalutView - is a reference to an object
    implementing the - AbstractView - interface (as the W3C spec says it
    should be) but not the global/window object.

    Richard.
    Richard Cornford, Feb 23, 2006
    #11
  12. Richard Cornford wrote:

    > Csaba Gabor wrote:
    >> Lasse Reichstein Nielsen wrote:
    >>> You might be able to use document.defaultView, as some browsers
    >>> have that property point to the window object. It is not
    >>> specified that the defaultView should also be the global object,
    >>> or a window object, and it's not available in all browsers, so
    >>> it's not a good solution.

    >> Thanks for the tip, I had missed it before. I just tested it out
    >> on my Firefox 1.5 / Win XP Pro and it is working fine. ...

    >
    > And if you tried it out on IceBrowser it would fail reliably.
    > IceBrowser's - document.defalutView - is a reference to an object
    > implementing the - AbstractView - interface (as the W3C spec says it
    > should be) but not the global/window object.


    The object that can be referred to with the `window' property of the global
    object implements the AbstractView interface of W3C DOM Level 2 Views in
    Gecko-based UAs (and probably others except of IE), because that object has
    a `document' property referring an object that implements the DocumentView
    interface (has a `defaultView' property referring to an object that
    implements the AbstractView interface).

    An object implementing an interface does _not_ mean that this object
    has /only/ the properties and methods this interface provides. But
    for a conforming implementation it is required that is has /also/
    these properties, unless they are specified as optional.


    PointedEars
    Thomas 'PointedEars' Lahn, Feb 24, 2006
    #12
  13. Thomas 'PointedEars' Lahn wrote:
    <snip>
    > The object that can be referred to with the `window' property
    > of the global object implements the AbstractView interface of
    > W3C DOM Level 2 Views in Gecko-based UAs (and probably others ...

    <snip>

    The point that the - document.defalutView - may refer to the
    global/window object in many implementations was already made (with the
    implication that it is that object that then must implement the
    AbstractView interface).

    My point was that the DOM Views specification does not require any more
    than that - document.defalutView - refer to _an_ object implementing
    the - AbstractView - interface. It does not require that object to be
    the global/window object, and it does not forbid any number of other
    objects from implementing the - AbstractView - interface if they want
    to.

    It would be a mistake to assume that the global/window object was an
    object implementing the - AbstractView - interface (particularly with
    regard to DOM Styles methods), or that the - document.defalutView -
    property was a reference from the document to the global/window object.
    At least it would be a mistake in a public Internet context, on a
    browser restricted Intranet it might be completely reasonable.

    Richard.
    Richard Cornford, Feb 25, 2006
    #13
  14. Richard Cornford wrote:

    > Thomas 'PointedEars' Lahn wrote:
    > <snip>
    >> The object that can be referred to with the `window' property
    >> of the global object implements the AbstractView interface of
    >> W3C DOM Level 2 Views in Gecko-based UAs (and probably others ...

    > <snip>
    >
    > The point that the - document.defalutView - may refer to the
    > global/window object in many implementations was already made (with the
    > implication that it is that object that then must implement the
    > AbstractView interface).
    >
    > My point was that the DOM Views specification does not require any more
    > than that - document.defalutView - refer to _an_ object implementing
    > the - AbstractView - interface. It does not require that object to be
    > the global/window object, and it does not forbid any number of other
    > objects from implementing the - AbstractView - interface if they want
    > to.


    Of course.

    > It would be a mistake to assume that the global/window object was an
    > object implementing the - AbstractView - interface (particularly with
    > regard to DOM Styles methods), or that the - document.defalutView -
    > property was a reference from the document to the global/window object.
    > At least it would be a mistake in a public Internet context, on a
    > browser restricted Intranet it might be completely reasonable.


    Whether I concur here would depend on what you mean by "assume". Certainly
    it is reasonable to feature-test it before using it, not following wild
    assumptions. However, if that object has a property named `document' that
    has a `defaultView' property that refers to the owner object, it is
    reasonable to assume that the interfaces were implemented as specified.


    PointedEars
    Thomas 'PointedEars' Lahn, Feb 25, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anze

    Internal IFrame?

    Anze, Oct 17, 2005, in forum: HTML
    Replies:
    8
    Views:
    983
    Neredbojias
    Oct 19, 2005
  2. HANM
    Replies:
    2
    Views:
    706
    Joseph Kesselman
    Jan 29, 2008
  3. Daedalous
    Replies:
    3
    Views:
    678
    Daedalous
    Jan 16, 2004
  4. Replies:
    0
    Views:
    129
  5. Replies:
    3
    Views:
    91
Loading...

Share This Page