Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get contents of iframe

Posted on 2011-03-15
13
Medium Priority
?
446 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:Tom 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:Tom 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:Tom 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Tom 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
 
LVL 5

Author Comment

by:Tom 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:Tom 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 1600 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:Tom 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:Tom 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 400 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:Tom Knowlton
ID: 35148949
thx
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses

783 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