javascript xhr加载iframe作为回应 [英] javascript xhr load iframe in response

查看:183
本文介绍了javascript xhr加载iframe作为回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何让这个XHR请求响应将.php文件加载到iframe而不是直接?

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://derrick.dk/ogmobi/facebook/contentLockerFacebook.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();


推荐答案

替换为:

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

这个:

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

或者,如果您只是想在iFrame中显示页面而不发出XHR请求。只需将其用作您的代码:

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);

这篇关于javascript xhr加载iframe作为回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆