Solved

Get contents of iframe

Posted on 2011-03-15
13
412 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Web Form Variables being reset 2 25
File Upload Control on a ASP.NET Overlay Page 1 42
Two different visual studio versions 3 21
Data is not showing from images 15 37
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

16 Experts available now in Live!

Get 1:1 Help Now