?
Solved

IFrame can't access / get IFRAME contents

Posted on 2004-11-27
17
Medium Priority
?
3,552 Views
Last Modified: 2008-01-09
Hi, I've been checking other solutions to this, and still can't get the IFRAME contents.


Please advise, this solution is not working for me: (browser id IE 6)

Here's what I use: <IFRAME>

<body>
<form id="form1">
<IFRAME src="http://www.google.com" name="myIframeId"></IFRAME>
</form>
<body>

<Javascript>
  Var vTheIFrameContents=top.frames['myIframeId'].document.documentElement.innerHTML;
#1) error returns: access denied

  Var vTheIFrameContents=top.frames['myIframeId'].Document.documentElement.innerHTML;
#2) object does not exist or is null

</Javascript>


...Thanks
0
Comment
Question by:paygo
  • 8
  • 7
  • 2
17 Comments
 
LVL 25

Expert Comment

by:devic
ID: 12686275
you can not acces document object on diefferent domain
0
 
LVL 25

Expert Comment

by:devic
ID: 12686279
try this:
===========================
<html>
<body onload=init()>
<hr>
<script>
function LoadPage(theURL,theID)
{


   var objSrvHTTP;
       objSrvHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
       objSrvHTTP.open ("GET",theURL,false);
       objSrvHTTP.send ("");

        document.getElementById(theID).innerHTML=objSrvHTTP.responseText;

}

function init()
{
      LoadPage("http://www.yahoo.com","yahoo")
      LoadPage("http://www.ebay.com","ebay")
}
</script>
<table width=555 cellspacing=0 cellpadding=0 border=3>
      <td valign=top id=yahoo></td>
      <td valign=top id=ebay></td>
</table>

</body>
</html>
0
 
LVL 25

Expert Comment

by:archrajan
ID: 12686284
You can't access documents on another server than the frame that the
script is in. So while a frame on
  host1.domain.tld
can call a function in another frame on
  host1.domain.tld
you can't call functions in another frame on
  host2.domain.tld
or even a frame in another domain e.g.
  host.domain2.tld

~ ~ ~

Actually, for version 4 and higher of IE and Netscape it *is* possible
for host1.domain.tld to access script elements from host2.domain.tld,
as long as pages on both host1 and host2 include the statement

document.domain = 'domain.tld';

A caveat, however, this is broken for Netscape 4.03 and 4.04.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Expert Comment

by:archrajan
ID: 12686311
SEE THIS FOR MORE INFO
With full DOM access we have two separate ways for two different
purposes to script an IFRAME.
Every IFRAME is contained as a window object in the
  window.frames
array (in both IE4+ and NN6 it seems), thus if you want to set the
location of an IFRAME or access its document and its content you
reference
  window.frames.iframeName
and then
  window.frames.iframeName.location.href
  window.frames.iframeName.document.links
etc.
If you want to access the HTMLIFrameElement the IFRAME tag creates you
access it with standard DOM methods:
  var iframeElement = document.getElementById('iframeId');
respectively
  var iframeElement = document.all['iframeId']
for IE4 and then you can set the html attributes e.g.
  iframeElement.width = 300;

The following contains examples of both uses: an empty IFRAME is
included in the page whose content is then change by getting the
iframe's window object and document.writing its document object. A
javascript link to access the iframe's content and another js link to
change the width of the iframe are provided.

<HTML>
<HEAD>
<SCRIPT>
var frameSrc = '';
frameSrc += '<HTML><BODY>';
frameSrc += '<BUTTON ONCLICK="alert(\'Kibology\');">';
frameSrc += 'button<\/BUTTON>';
frameSrc += '<\/BODY><\/HTML>';
var iframeWin, iframeElement;
function init () {
  iframeWin = window.frames.anIframe;
  iframeWin.document.open();
  iframeWin.document.write(frameSrc);
  iframeWin.document.close();
  iframeElement =
    document.getElementById ?
      document.getElementById('anIframe') :
      document.all['anIframe'];
}
</SCRIPT>


</HEAD>
<BODY ONLOAD="init();">
<A HREF="javascript: alert
(iframeWin.document.body.firstChild.nodeName); void 0">
show iframe first child
</A>
|
<A HREF="javascript: iframeElement.width = 300; void 0">
set width to 300
</A>
<BR>
<IFRAME ID="anIframe" NAME="anIframe" SRC="about:blank" WIDTH="100">
</IFRAME>
</BODY>
</HTML>
0
 

Author Comment

by:paygo
ID: 12686550
Basic question on domains,

Once this frame loads (using google as the source page):

<IFRAME src="http://www.google.com" name="myIframeId"></IFRAME>

and the content is captured in the IFrame (i.e the received html page), isn't that not
part of my local browers AND my local domain ?  

It would seem an easy step just to read the captured HTML contents from my local browser,
just like accessing the value of a text field?

...thanks  
0
 

Author Comment

by:paygo
ID: 12686560
RESTATE
Basic question on domains,

Once this frame loads (using google as the source page):

<IFRAME src="http://www.google.com" name="myIframeId"></IFRAME>

Once the content is captured in the IFrame (i.e the received html page), isn't that not
part of my local browers (client)  AND my local domain ?  

It would seem an easy step just to read the captured HTML contents from my local browser (client),
just like accessing the value of a text field?

...thanks  
0
 
LVL 25

Expert Comment

by:devic
ID: 12686577
>>>Once the content is captured in the IFrame (i.e the received html page), isn't that not
part of my local browers (client)  AND my local domain ?

no.

if you need this only for you and only for local computer, then rename your *.html file to *.hta and it will work.
because HTA application can acces a different domain.
0
 

Author Comment

by:paygo
ID: 12690407
Thanks for the help.

Using the 'new ActiveXObject("Msxml2.XMLHTTP.4.0")' instead of the frames method returns
2 messages - that I would need to avoid.

IS there any way to avoid / bypass these two messages?

1) "The page is accessing Information that is not under it's control. This poses asecurity risk"

2) MSXML4.ACCESS is denied - (can't get to the page after this one)

Thanks
0
 
LVL 25

Expert Comment

by:devic
ID: 12690421
unfortunately no, only on server side
0
 

Author Comment

by:paygo
ID: 12690555
Devisc thanks for the help - Are there any solutions to this?
Here's what I've found out so far.

1) I can't get to the HTML contents of an IFRAME that is pointing to a URL such as src=http://www.yahoo.com.

2) I can't use the ActiveXObject("Msxml2.XMLHTTP.4.0")
since A) Access is denied and b) this poses a security risk.

so.

Since I can see the source HTML loaded correctly in the IFRAME, is there a way to screen scrape the

contents using asp.net?

Thanks



 

0
 
LVL 25

Accepted Solution

by:
devic earned 2000 total points
ID: 12690608
Paygo, as I said before on server side it can be done.
and with ASP and with PHP etc.

an example for ASP:
==============================
<%
Function GetContent(url)
    Dim xmlHttp
    Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xml.Open "GET", url, True
    Call xml.Send()

      If xml.readyState <> 4 Then
        xml.waitForResponse 3
    End If
   
     If Err.Number <> 0 Then
    Else
         If (xml.readyState <> 4) Or (xml.Status <> 200) Then
              xml.Abort
              strData = "Problem communicating with remote server..."
         Else
              strData = xml.ResponseText
         End If
    End If
    GetContent = strData
End Function
%>

<%=GetContent("http://oldlook.experts-exchange.com/")%>
0
 

Author Comment

by:paygo
ID: 12691219
Does anyone have this solution in C# scripting - instead of VB?
0
 
LVL 25

Expert Comment

by:devic
ID: 12691229
I don't have, wait a while I do it.
0
 
LVL 25

Expert Comment

by:devic
ID: 12691405
here an example:

you needd add COM reference: Project --> Add Reference --> COM -->
there find Microsoft.Xml, 5.0 or another version.




then declare it:
=========================
using MSXML2;





and go:
=========================

                  string postData = "";
                  string uriString = "http://www.sembel.net";
                  String strXML ;

                  XMLHTTPClass xmlHTTP = new XMLHTTPClass();

                  xmlHTTP.open("POST", uriString,false,null,null) ;
                  xmlHTTP.send(postData) ;

                  while(xmlHTTP.readyState != 4)
                  {
                  }

                  strXML = (string)xmlHTTP.responseText;
                  
      
0
 

Author Comment

by:paygo
ID: 12692046
Hi Devic,

I have 2 other similar questions posted for 1000 points,  
(look up:Server.CreateObject("MSXML2.ServerXMLHTTP")

I tried Http://www.sembel.net using the above code  - this works fine.

I tried Http://www.cnn.com and received this error:

"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL / was not found on this server.</p>\n<hr />\n<address>Apache Server at www.cnn.com Port 80</address>\n</body></html>\n"

Thanks
0
 
LVL 25

Expert Comment

by:devic
ID: 12692125
maybe the server checks something, e.g. cookie or referrer

check for example:
http://www.bbc.co.uk
0
 

Author Comment

by:paygo
ID: 12692838
The server I will access will check to see if Javascript is enabled.

This is why I started out using an Iframe -  thx
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question