Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iFrame : How to display a server response (HTML) directly into iFrame using javascript?

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

like image 208
Gendaful Avatar asked Mar 03 '26 03:03

Gendaful


1 Answers

With vanilla JavaScript:

document.MyFrame.document.body.innerHTML = serverResponse

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

like image 104
Diodeus - James MacFarlane Avatar answered Mar 04 '26 17:03

Diodeus - James MacFarlane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!