使用纯JavaScript获取Iframe的内容 [英] Getting Contents of Iframe with Pure JavaScript

查看:107
本文介绍了使用纯JavaScript获取Iframe的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经可以使用jQuery获取iframe的内容,不过我想学习如何使用纯JavaScript获取它。

I already can get the content of an Iframe with jQuery, though I would like to learn how to get it with pure JavaScript.

这就是我所拥有的远。

 var frame = document.getElementById('awc_frame');
 var easyBB = frame.contentWindow.document.body.innerHTML;
 easyBB.getElementById('chatbox-title').innerText="Chatbox";

我做错了什么,请帮忙。最初框架也没有ID,我已经尝试过这个

What am I doing wrong, please help. Also originally the frame does not have an ID, and I already tried this

var frame = document.frames['awc_frame'];

交叉浏览器有效吗?然后我如何获得contentWindow?只是一些解释,所以我可以用JavaScript而不是jQuery来做到这一点。 jQuery版本是这个

Is that cross browser efficient? And then how do I get the contentWindow? Just some explanation so I can do this with JavaScript and not jQuery. jQuery version is this

var frame = $('#avacweb_chat iframe');
var easyBB = $('#chatbox-title',frame.contents()).text('Chatbox');


推荐答案

如果它位于同一个域中,请尝试此操作。如果iframe与您正在查看的窗口的来源不同,则不允许您访问iframe内容。

If it is on the same domain, try this. It won't let you access iframe contents if the iframe is of a different origin than the window that you are viewing.

var iframe = document.getElementById("awc_frame");
var iframe_contents = iframe.contentDocument.body.innerHTML;

jsfiddle iframe查看同一域名页面的工作示例:

Working example with jsfiddle iframe viewing a page on the same domain:

http://jsfiddle.net/tqAL3/1/

这篇关于使用纯JavaScript获取Iframe的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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