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

J

John L.

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

Tim Streater

John L. said:
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.
 
J

John L.

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

Denis McMahon

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
 
P

Peter May

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
 
D

Dr J R Stockton

In comp.lang.javascript message <ce31d781-32e1-459e-bc99-989eba296ed1@h9
g2000vbr.googlegroups.com>, Fri, 2 Sep 2011 09:25:04, John L.
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, 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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top