Solved

Get contents of iframe

Posted on 2011-03-15
13
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 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: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 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:Tom Knowlton
ID: 35148949
thx
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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