Javascript Iframe insideHTML
有谁知道如何从 IFRAME 中获取 HTML 我尝试了几种不同的方法:
document.getElementById('iframe01').contentDocument.body.innerHTML
document.frames['iframe01'].document.body.innerHTML
document.getElementById('iframe01').contentWindow.document.body.innerHTML
等等
Does anyone know how to get the HTML out of an IFRAME I have tried several different ways:
document.getElementById('iframe01').contentDocument.body.innerHTML
document.frames['iframe01'].document.body.innerHTML
document.getElementById('iframe01').contentWindow.document.body.innerHTML
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我认为这就是你想要的:
编辑:
我有充分的权威证明,这在 Chrome 和 Firefox 中不起作用,尽管它在 IE 中运行得很好,这是我测试它的地方。 回想起来,这是一个很大的错误
这会起作用:
我知道这并不完全是所要求的,但不想删除答案,因为我认为它有一席之地。
我喜欢下面 @ravz 的 jquery 答案。
I think this is what you want:
EDIT:
I have it on good authority that this won't work in Chrome and Firefox although it works perfectly in IE, which is where I tested it. In retrospect, that was a big mistake
This will work:
I understand that this isn't exactly what was asked but don't want to delete the answer because I think it has a place.
I like @ravz's jquery answer below.
有类似下面的东西会起作用。
Having something like the following would work.
如果您查看 JQuery,您可以执行以下操作:
这假设您的 iframe 父级和子级位于由于 Javascript 中的同源策略,同一服务器。
If you take a look at JQuery, you can do something like:
This is assuming that your iframe parent and child reside on the same server, due to the Same Origin Policy in Javascript.
康罗伊的回答是正确的。 如果您只需要 body 标记中的内容,只需使用:
Conroy's answer was right. In the case you need only stuff from body tag, just use:
您可以使用 contentDocument 或 contentWindow 属性来实现此目的。
这是示例代码。
这里,
myframe
是您的 iframe 的 ID。注意:您无法从域外的 src 中提取 iframe 中的内容。
You can use the contentDocument or contentWindow property for that purpose.
Here is the sample code.
here,
myframe
is the id of your iframe.Note: You can't extract the content out of an iframe from a src outside you domain.
不要忘记,出于安全考虑,您不能跨域。
因此,如果是这种情况,您应该使用 JSON。
Don't forget that you can not cross domains because of security.
So if this is the case, you should use JSON.
该解决方案的工作原理与 iFrame 相同。 我创建了一个 PHP 脚本,可以从其他网站获取所有内容,最重要的部分是您可以轻松地将自定义 jQuery 应用于该外部内容。 请参考以下脚本,可以从其他网站获取所有内容,然后您也可以应用您自定义的 jQuery/JS。 该内容可以在任何地方、任何元素或任何页面内使用。
This solution works same as iFrame. I have created a PHP script that can get all the contents from the other website, and most important part is you can easily apply your custom jQuery to that external content. Please refer to the following script that can get all the contents from the other website and then you can apply your cusom jQuery/JS as well. This content can be used anywhere, inside any element or any page.
如果您在 Firefox 上安装 ForceCORS 过滤器,则可以从其他域获取源。 当您打开此过滤器时,它将绕过浏览器中的安全功能,即使您尝试阅读另一个网页,您的脚本也将正常工作。 例如,您可以在 iframe 中打开 FoxNews.com,然后阅读其源代码。 现代 Web 浏览器默认拒绝此功能的原因是,如果另一个域包含一段 JavaScript,而您正在阅读该 JavaScript 并将其显示在您的页面上,则它可能包含恶意代码并构成安全威胁。 因此,每当您在页面上显示来自另一个域的数据时,您都必须提防这种真正的威胁,并在显示文本之前实施一种方法来过滤掉文本中的所有 JavaScript 代码。 请记住,当一段假定的原始文本包含一些包含在脚本标记中的代码时,当您将其显示在页面上时,它们不会显示,但它们会运行! 所以,认识到这是一个威胁。
http://www-jo.se/f.pfleger/forcecors
You can get the source from another domain if you install the ForceCORS filter on Firefox. When you turn on this filter, it will bypass the security feature in the browser and your script will work even if you try to read another webpage. For example, you could open FoxNews.com in an iframe and then read its source. The reason modern web brwosers deny this ability by default is because if the other domain includes a piece of JavaScript and you're reading that and displaying it on your page, it could contain malicious code and pose a security threat. So, whenever you're displaying data from another domain on your page, you must beware of this real threat and implement a way to filter out all JavaScript code from your text before you're going to display it. Remember, when a supposed piece of raw text contains some code enclosed within script tags, they won't show up when you display it on your page, nevertheless they will run! So, realize this is a threat.
http://www-jo.se/f.pfleger/forcecors
您可以使用此代码从 iframe 中获取 html
iframe = document.getElementById('frame');
innerHtml = iframe.contentDocument.documentElement.innerHTML
You can get html out of an iframe using this code
iframe = document.getElementById('frame');
innerHtml = iframe.contentDocument.documentElement.innerHTML