Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript xhr load iframe in response

Does anyone have an idea how to make this XHR request response load the .php file into an iframe as opposed to directly?

var xhr= new XMLHttpRequest();
    xhr.open('GET', 'http://example.com/foo/bar/baz.php', true);
    xhr.onreadystatechange= function() {
        if (this.readyState!==4) return;
        if (this.status!==200) return;
        document.getElementsByTagName('body')[0].innerHTML = document.getElementsByTagName('body')[0].innerHTML + this.responseText;    
};
xhr.send();
like image 380
yella-dom Avatar asked Sep 02 '15 21:09

yella-dom


1 Answers

Replace this:

document.getElementsByTagName('body')[0].innerHTML = document.getElementsByTagName('body')[0].innerHTML + this.responseText;

with this:

var iframe = document.createElement('iframe');
iframe.srcdoc = this.responseText;
iframe.src = "data:text/html;charset=utf-8," + escape(this.responseText);
document.body.appendChild(iframe);

Alternatively, if you just want to display the page in an iFrame without making the XHR request. Just use this as your code:

var iframe = document.createElement('iframe');
iframe.src = 'http://derrick.dk/ogmobi/facebook/contentLockerFacebook.php';
document.body.appendChild(iframe);
like image 59
Joshua Dwire Avatar answered Nov 01 '22 17:11

Joshua Dwire