2

I am getting a simple server response which is an html file and I want to display the same in iFrame without saving the file to my workspace or machine.

I am making an ajax call as below.

                   Ext.Ajax.request({
                        url : 'url',
                        method : 'POST',
                        success : function(response) {

                        var responseHtmlStr =response.responseText;

Sample Server response which I am getting in responseHtmlStr is as below.

<!DOCTYPE html>
<html>
<head>
<script>
function copyText()
{
alert('It is clicked');
}
</script>
</head>
<body>

Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2">
<br><br>
<button onclick="copyText()">Copy Text</button>

</body>
</html>

The code I am using to create the iFrame is as below.

;

As I dont want to store the server response in the server in a file. How to directly feed the server response in the iFrame?

I tried document.getElementsByTagName('iframe')[0].contentWindow.document.write(serverResponse);​ but it is not working with the above code.

Any other suggestions please. Thanks in advance.

Thanks Gendaful

Gendaful
  • 5,522
  • 11
  • 57
  • 76

2 Answers2

6

With vanilla JavaScript:

document.MyFrame.document.body.innerHTML = serverResponse

..where you have <iframe name="MyFrame"></iframe>

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

Not sure if this will work, I do something similar using jquery (but could do it just as easily without)...

$('iframe').contents().find("html")
    .append($("<head></head><body> blah blah blah </body>"));
SimpleSam5
  • 3,280
  • 2
  • 16
  • 13