Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Accessing the HTML source of a page loaded in an IFRAME in top-level JavaScript

Reply
Thread Tools

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

 
 
John L.
Guest
Posts: n/a
 
      09-02-2011
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").documen t.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.
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      09-02-2011
In article
<ce31d781-32e1-459e-bc99->,
"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.documen t......

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
 
Reply With Quote
 
 
 
 
John L.
Guest
Posts: n/a
 
      09-02-2011
I've tried multiple variations with self, window, document, body, etc.
with no luck. Anyone else?
 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      09-02-2011
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
 
Reply With Quote
 
Peter May
Guest
Posts: n/a
 
      09-02-2011
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").documen t.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/forum...n-firefox.html

--
Peter
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      09-03-2011
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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-04-2011
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$>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cross-scripting issue: use javascript/DOM on iframe loaded from differentdomain, how? Mike S Javascript 10 01-02-2012 08:47 AM
Wrong page loaded into IFrame object (strange problem) Benny Shoham ASP .Net 1 04-12-2007 11:50 AM
parent/iframe set iframe source and submit form superfreaker@gmail.com Javascript 2 03-09-2006 11:05 PM
How do we load page into iframe when not on iframe page? Paul Javascript 3 07-04-2005 06:59 AM
Get form values from iframe (1) to iframe (2) inside a layer in iframe (1) Daedalous Javascript 3 01-16-2004 11:08 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57