使用纯 JavaScript 获取 iframe 的内容 [英] Getting Contents of Iframe with Pure JavaScript
问题描述
我已经可以使用 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:
这篇关于使用纯 JavaScript 获取 iframe 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!