Solved

Get contents of iframe

Posted on 2011-03-15
13
405 Views
Last Modified: 2012-05-11
I have an HTML control, an iframe, that I am setting the SRC at runtime.

Is it possible to obtain the HTML souce code of the iframe later on?
0
Comment
Question by:knowlton
  • 8
  • 4
13 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35143107
You should be able to fetch the innerHTML property of the iframe, something like this:

window.frames['iframe01'].document.body.innerHTML

Depending on the security settings and the domain of the target of the iframe you may get a cross-domain security restriction.
0
 
LVL 5

Author Comment

by:knowlton
ID: 35143126
I see :/

Yeah...InnerHtml is empty.



I am open to using something other than an iframe, also.

Are there other controls that behave like an iframe, but allow you to view the HTML Source Code?



I mean -- I can see the information right there!!   I just can't seem to get to it via the iframe, even though I set id and runat="server" so I can interact with it in the codebehind.


--------------------


Alternatively, is there a way to intercept the HTML coming into the iframe?  Like some sort of custom handler or some such?


Thanks.
0
 
LVL 5

Author Comment

by:knowlton
ID: 35143149
NOTE:  I am hoping to do this from the C# codebehind, not via javascript.

Also, the URL of the iframe src attribute is from another server I do not control.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35143160
Are you waiting for the iframe to actually load before fetching the inner HTML?

You may need to put in some synchronization code, or loop until it comes back non-null with a 1-second timer.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35143169
If the src is from another server you will probably not be able to fetch the innerHTML, due to cross-domain security restrictions which are enabled by default.
0
 
LVL 5

Author Comment

by:knowlton
ID: 35143171
>>>Are you waiting for the iframe to actually load before fetching the inner HTML?

Not sure if I am.  Good question.

>>>You may need to put in some synchronization code, or loop until it comes back non-null with a 1-second timer.


Hmmmm.......
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Author Comment

by:knowlton
ID: 35143188
>>>>If the src is from another server you will probably not be able to fetch the innerHTML, due to cross-domain security restrictions which are enabled by default.


Sure, but if I can see the output -- something tells me I can (or should) be able to GET TO that information programmatically!!

For example.... in Mozilla, I can right click inside the iframe and go to This Frame ... View Frame Source ... and I can SEE the info I am looking for!!!

So I don't see how the HTML is really being kept from view....


I just want to do the same thing programmatically.
0
 
LVL 5

Author Comment

by:knowlton
ID: 35143193
Can the iframe itself raise an event when it is finished loading the SRC url?
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 400 total points
ID: 35143287
The browser security is designed to keep information like innerHTML from leaking from one domain to another, so any trick we find will have to be just that - a trick.

What are you trying to get from the source of the iframe and can it be done on the back end by fetching the URL directlly?

The iframe does have an onload callback by which time you should be able to view the innerHTML, security aspect aside.

function GetInnerHTML()
{
...
}

<iframe...  onload="GetInnerHTML()">
0
 
LVL 5

Author Comment

by:knowlton
ID: 35143307
function GetInnerHTML()
{
...
}

<iframe...  onload="GetInnerHTML()">

Open in new window



Understood.



I understand the concerns about security.  In this particular case, nothing nefarious is afoot, just me trying to get some information off a web page that happens to be in an iframe.


I may have some other work-arounds, via some exposed web services from the same vendor who is hosting the payments page (displayed in the iframe).
0
 
LVL 5

Author Comment

by:knowlton
ID: 35143321
I think the points are yours.

I want to give it a few days, see what other folks have to say.
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 100 total points
ID: 35144458
Hi, go through this example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
<script type="text/javascript">
function uploadform()
{
    alert(document.getElementById('target_upload').innerHTML);
    document.getElementById('text').innerHTML = document.getElementById('target_upload').innerHTML;
}
</script>
</head>


<body>

<iframe onload="uploadform();" id='target_upload' name='target_upload' src='' style='width:500px;height:150px;border:1'></iframe>

<form action="http://server2.com/index.php" name="upload" action="" method="POST" enctype="multipart/form-data" target="target_upload">
<input name="fileupload" type="file" size="30" />
<input type="button" name="test" value="test" onclick="uploadform();" />
<input type="submit" name="submit" value="Submit" />

</form>
<span id="text">
</span>

</body>

</html>
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 35148949
thx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now