简体   繁体   English

将iframe中的html内容复制到div(ajax)中?

[英]Copy html content from iframe into div ( ajax )?

Lets assume I have my browser load an Iframe with <iframe src="test.html"> 让我们假设我的浏览器加载了iframe与<iframe src="test.html">

Can I, using ajax, load the content of test.html into a div in the main html page? 我可以使用ajax将test.html的内容加载到主html页面的div中吗?

This idea is my solution for that fact that I'm actually trying to overcome the limitation with making ajax submits to remote hosts. 这个想法是我的解决方案,我实际上试图通过使ajax提交给远程主机来克服限制。 The plan is to generate the dynamic page with 0 sized iframe which makes report request to remote host. 计划是生成具有0大小的iframe的动态页面,该页面向远程主机发出报告请求。 Then, after the page (& iframe content) loads I will copy the iframe content into a div using JS. 然后,在页面(&iframe内容)加载后,我将使用JS将iframe内容复制到div中。

Tips are appreciated, 提示表示赞赏,

Thank you, Maxim. 谢谢你,马克西姆。

No, you can't. 不,你不能。

When you load a page from a different domain into the iframe, it becomes unreachable. 将页面从其他域加载到iframe时,它将无法访问。 You can no longer access the contents of the iframe, as it comes from a different domain. 您无法再访问iframe的内容,因为它来自不同的域。

The only thing that I know of that you can reliably load from a different domain is a script, which JSONP uses. 我所知道的唯一可以从不同域可靠加载的是JSONP使用的脚本。

Can I, using ajax, load the content of test.html into a div in the main html page? 我可以使用ajax将test.html的内容加载到主html页面的div中吗?

Yes (since your example has a relative URI and is on the same host) … 是(因为您的示例具有相对URI并且位于同一主机上)...

This idea is my solution for that fact that I'm actually trying to overcome the limitation with making ajax submits to remote hosts. 这个想法是我的解决方案,我实际上试图通过使ajax提交给远程主机来克服限制。

… and no. … 和不。 You still can't read data from remote hosts. 您仍然无法从远程主机读取数据。

I'm sure someone will correct me if I'm wrong, but I believe that scripting across domain boundaries is restricted. 我确信如果我错了,有人会纠正我,但我相信跨域边界的脚本是有限的。 Have you tried it? 你试过吗? Here's a function that may help out. 这是一个可能有用的功能。

function insertDivFromFrame(divname, framename) {
    var frame = document.getElementById(framename);
    var d = frame.contentWindow || frame.contentDocument;
    if (oDoc.document) {d = d.document;}
    document.getElementById('yourdiv').innerHTML = d.body.innerHTML;
}

I'm not sure this code works... see http://xkr.us/articles/dom/iframe-document/ for more help on this. 我不确定这段代码是否有效...请参阅http://xkr.us/articles/dom/iframe-document/以获取更多帮助。

...您可能,但是,设计一个AJAX请求到本地主机和检索远程服务器的信息(如描述在这里 )。

If you write a php/perl/etc. 如果你写一个php / perl / etc。 script to output the contents of a document from another domain, it'll give you access to the contents as the resulting page would be considered by javascript to belong to your domain. 脚本从另一个域输出文档的内容,它将允许您访问内容,因为结果页面将被javascript视为属于您的域。 If you're not familiar with any server-side scripting languages, I'm sure you'd be able to find a script that'll do this for you by doing a simple google search. 如果您不熟悉任何服务器端脚本语言,我相信您可以通过简单的谷歌搜索找到一个可以为您执行此操作的脚本。

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM