views:

140

answers:

3

Is there any way that I can change the content of iframe from a cached html string? I am not talking about iframe.Attribute["src"] = "url", but rather given a string of html and change the content.

An ugly workaround might be saving the html into a file and change iframe src to it. Any other ways? Maybe javascript can help?

+2  A: 

If you can control the contents of the iframe html, you could do something like this:

Expose a method as such:

<script type = "text/javascript">
function setData(str)
{
$('html').html(str);
}
</script>

and call it like

document.iFrameName.setData(myHtmlStr);

from the parent frame.

Stefan Kendall
This requires jQuery, of course.
Stefan Kendall
I'm using C#, how to I get a C# string passed to javascript setData(str)?
Yang
A: 

Stefan Kendall's solution is a good one. As a fallback, if you can't or don't want to use jQuery, you can also document.write into the iframe. Here's an example which I believe will eliminate all the cross-browser quirks.

Pekka
A: 

Bearing in mind this must be on the same domain, otherwise you're into rpc territory...

lphmedia