Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get contents of iframe

Posted on 2011-03-15
13
Medium Priority
?
443 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
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.

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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…

670 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