Accessing the HTML source of a page loaded in an IFRAME in top-level JavaScript

Discussion in 'Javascript' started by John L., Sep 2, 2011.

  1. John L.

    John L. Guest

    Is it possible to access the HTML source of a page that is loaded in
    an IFRAME via JavaScript in the top-level page? I thought something
    like this would work:

    <HTML>
    <IFRAME id="myIFRAME" src="B.html"></IFRAME>
    <SCRIPT>
    var a =
    window.document.getElementById("myIFRAME").document.body.innerHTML;
    </SCRIPT>
    </HTML>

    to get the HTML source of B.HTML into a JavaScript variable, but alas,
    no luck.

    Can anyone offer any guidance? Thanks in advance.
     
    John L., Sep 2, 2011
    #1
    1. Advertising

  2. John L.

    Tim Streater Guest

    In article
    <>,
    "John L." <> wrote:

    > Is it possible to access the HTML source of a page that is loaded in
    > an IFRAME via JavaScript in the top-level page? I thought something
    > like this would work:


    > window.document.document.body.innerHTML;


    You may need;

    document.getElementById("myIFRAME").window.document......

    but I can't remember now. IME I just played around until it worked.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
     
    Tim Streater, Sep 2, 2011
    #2
    1. Advertising

  3. John L.

    John L. Guest

    Re: Accessing the HTML source of a page loaded in an IFRAME intop-level JavaScript

    I've tried multiple variations with self, window, document, body, etc.
    with no luck. Anyone else?
     
    John L., Sep 2, 2011
    #3
  4. Re: Accessing the HTML source of a page loaded in an IFRAME intop-level JavaScript

    On Fri, 02 Sep 2011 11:01:06 -0700, John L. wrote:

    > I've tried multiple variations with self, window, document, body, etc.
    > with no luck. Anyone else?


    Is the iframe populated from the same website as the parent page?

    You may be hitting a designed in security boundary!

    For example, when a shop webpage loads a card verification page in a
    frame, the shop website can not access data inside the frame.

    Otherwise, any online shop you visited would be able to read your card
    password.

    Rgds

    Denis McMahon
     
    Denis McMahon, Sep 2, 2011
    #4
  5. John L.

    Peter May Guest

    Re: Accessing the HTML source of a page loaded in an IFRAME in top-levelJavaScript

    W dniu 02-09-2011 18:25, John L. pisze:
    > Is it possible to access the HTML source of a page that is loaded in
    > an IFRAME via JavaScript in the top-level page? I thought something
    > like this would work:
    >
    > <HTML>
    > <IFRAME id="myIFRAME" src="B.html"></IFRAME>
    > <SCRIPT>
    > var a =
    > window.document.getElementById("myIFRAME").document.body.innerHTML;
    > </SCRIPT>
    > </HTML>
    >
    > to get the HTML source of B.HTML into a JavaScript variable, but alas,
    > no luck.
    >
    > Can anyone offer any guidance? Thanks in advance.


    I would try:

    var iframeObj = document.getElementById('my_iframe_id'),
    doc = (iframeObj.contentWindow || iframeObj.contentDocument),
    body, content;

    if(doc.document)
    {
    body = doc.document.getElementsByTagName('body')[0];
    content = body.outerHTML;
    }

    For Firefox try to find equivalent, for example:
    http://www.velocityreviews.com/forums/t518068-outerhtml-not-working-in-firefox.html

    --
    Peter
     
    Peter May, Sep 2, 2011
    #5
  6. In comp.lang.javascript message <ce31d781-32e1-459e-bc99-989eba296ed1@h9
    g2000vbr.googlegroups.com>, Fri, 2 Sep 2011 09:25:04, John L.
    <> posted:

    >Is it possible to access the HTML source of a page that is loaded in
    >an IFRAME via JavaScript in the top-level page?



    Yes.

    See the code for <http://www.merlyn.demon.co.uk/linxchek.htm>. Note
    that only a local copy of the page can be run, since I don't want others
    to waste my bandwidth on checking my links.

    IIRC, the inner page must be from the same domain as the outer; and when
    using Chrome after about version 5 they cannot be local files (they
    probably can be from a local server).

    FYI, here's a pane of possible results of that page (the 4 bad dates are
    test cases for the bad date tester):-


    Consolidation :
    ..
    .. Targets missing : 0
    .. Anchors not used : 0
    .. Anchors repeated : 0
    .. ID dupes Target : 0
    .. FolderMissing - Linked and Missing, 0 cites of 0 places
    .. LinkedMissing - Linked and Missing, 0 cites of 0 places
    .. Date_Problems - Bad Date / Bad Day, 4 cites of 4 places
    ..
    .. Unlike8point3 - Vexing Name Format, 28 cites of 5 places
    .. Domains_Cited - Domains Linked For, 2805 cites of 1059 places
    .. LocalOutLinks - X-site Local Files, 65 cites of 46 places
    .. AllLocalLinks - Local Files Linked, 11555 cites of 3896 places
    .. AllLocalFiles - Total Files Linked, 11555 cites of 446 places
    .. FolderPresent - Linked and Present, 25 cites of 4 places
    .. LinkedPresent - Linked and Present, 11465 cites of 396 places
    .. All_LinkTexts - All Texts of Links, 14077 cites of 6265 places
    .. SrcAttributes - 'SRC=' Attributes, 425 cites of 85 places
    .. Anchors sighted : 3292
    .. IDs sighted : 785
    .. SRCs sighted : 425
    .. Links, total : 14078
    .. Links, other : 2948
    .. Protocols : c: 1, file: 11555, ftp: 109, http: 2658, https: 36,
    javascript: 5, mailto: 4, news: 116, view-source: 19
    .. Extensions : awk 1, bat 8, bmp 1, css 7, exe 8, gif 230, htm 10546,
    jpg 11, js 237, null 1, pas 127, pl 1, png 8, shtml 15, txt 194, xhtml
    2, zip 68
    .. Time taken : 68536 ms.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op12 Sf5 Cr12
    news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
    <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Sep 3, 2011
    #6
  7. Dr J R Stockton wrote:

    > John L. posted:
    >> Is it possible to access the HTML source of a page that is loaded in
    >> an IFRAME via JavaScript in the top-level page?

    >
    > Yes.
    >
    > […]
    > IIRC, the inner page must be from the same domain as the outer; and when
    > using Chrome after about version 5 they cannot be local files (they
    > probably can be from a local server).


    The Same Origin Policy, which applies here, is properly explained at
    <http://code.google.com/p/browsersec/wiki/Part2#Same-origin_policy>.

    Bottom line is that in most browsers the protocol (that is, the scheme part
    of the URI), the domain (that is, the hier-part of the URI without the `:'
    and the port number, if any), and the port number of the accessing and the
    accessed resource must match; `document.domain' can sometimes be used to
    allow access for a resource from a different domain within the same second-
    level domain. If one of the resources is accessed with a `file://' URI, so
    must be the other for the access to work (this security measure prevents
    scripts on Web sites from spying on local resources).


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Sep 4, 2011
    #7
    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. pabbu
    Replies:
    8
    Views:
    730
    Marc Boyer
    Nov 7, 2005
  2. musosdev
    Replies:
    5
    Views:
    357
    musosdev
    Sep 2, 2008
  3. Robert Oschler
    Replies:
    4
    Views:
    233
    Csaba2000
    Aug 26, 2003
  4. Daedalous
    Replies:
    3
    Views:
    706
    Daedalous
    Jan 16, 2004
  5. nc
    Replies:
    0
    Views:
    147
Loading...

Share This Page