Javascript Iframe insideHTML

发布于 2024-07-06 22:38:00 字数 273 浏览 5 评论 0原文

有谁知道如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

〆一缕阳光ご 2024-07-13 22:38:00

我认为这就是你想要的:

window.frames['iframe01'].document.body.innerHTML 

编辑:

我有充分的权威证明,这在 Chrome 和 Firefox 中不起作用,尽管它在 IE 中运行得很好,这是我测试它的地方。 回想起来,这是一个很大的错误

这会起作用:

window.frames[0].document.body.innerHTML 

我知道这并不完全是所要求的,但不想删除答案,因为我认为它有一席之地。

我喜欢下面 @ravz 的 jquery 答案。

I think this is what you want:

window.frames['iframe01'].document.body.innerHTML 

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:

window.frames[0].document.body.innerHTML 

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.

百变从容 2024-07-13 22:38:00

有类似下面的东西会起作用。

<iframe id = "testframe" onload = populateIframe(this.id);></iframe>

// The following function should be inside a script tag

function populateIframe(id) { 

    var text = "This is a Test"
var iframe = document.getElementById(id); 

var doc; 

if(iframe.contentDocument) { 
    doc = iframe.contentDocument; 
} else {
    doc = iframe.contentWindow.document; 
}

doc.body.innerHTML = text; 

  }

Having something like the following would work.

<iframe id = "testframe" onload = populateIframe(this.id);></iframe>

// The following function should be inside a script tag

function populateIframe(id) { 

    var text = "This is a Test"
var iframe = document.getElementById(id); 

var doc; 

if(iframe.contentDocument) { 
    doc = iframe.contentDocument; 
} else {
    doc = iframe.contentWindow.document; 
}

doc.body.innerHTML = text; 

  }
‘画卷フ 2024-07-13 22:38:00

如果您查看 JQuery,您可以执行以下操作:

<iframe id="my_iframe" ...></iframe>

$('#my_iframe').contents().find('html').html();

这假设您的 iframe 父级和子级位于由于 Javascript 中的同源策略,同一服务器。

If you take a look at JQuery, you can do something like:

<iframe id="my_iframe" ...></iframe>

$('#my_iframe').contents().find('html').html();

This is assuming that your iframe parent and child reside on the same server, due to the Same Origin Policy in Javascript.

萌面超妹 2024-07-13 22:38:00

康罗伊的回答是正确的。 如果您只需要 body 标记中的内容,只需使用:

$('#my_iframe').contents().find('body').html();

Conroy's answer was right. In the case you need only stuff from body tag, just use:

$('#my_iframe').contents().find('body').html();
不再见 2024-07-13 22:38:00

您可以使用 contentDocument 或 contentWindow 属性来实现此目的。
这是示例代码。

function gethtml() {
  const x = document.getElementById("myframe")
  const y = x.contentWindow || x.contentDocument
  const z = y.document ? y.document : y
  alert(z.body.innerHTML)
}

这里,myframe 是您的 iframe 的 ID。
注意:您无法从域外的 src 中提取 iframe 中的内容。

You can use the contentDocument or contentWindow property for that purpose.
Here is the sample code.

function gethtml() {
  const x = document.getElementById("myframe")
  const y = x.contentWindow || x.contentDocument
  const z = y.document ? y.document : y
  alert(z.body.innerHTML)
}

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.

坏尐絯℡ 2024-07-13 22:38:00

不要忘记,出于安全考虑,您不能跨域。

因此,如果是这种情况,您应该使用 JSON

Don't forget that you can not cross domains because of security.

So if this is the case, you should use JSON.

野の 2024-07-13 22:38:00

该解决方案的工作原理与 iFrame 相同。 我创建了一个 PHP 脚本,可以从其他网站获取所有内容,最重要的部分是您可以轻松地将自定义 jQuery 应用于该外部内容。 请参考以下脚本,可以从其他网站获取所有内容,然后您也可以应用您自定义的 jQuery/JS。 该内容可以在任何地方、任何元素或任何页面内使用。

<div id='myframe'>

  <?php 
   /* 
    Use below function to display final HTML inside this div
   */

   //Display Frame
   echo displayFrame(); 
  ?>

</div>

<?php

/* 
  Function to display frame from another domain 
*/

function displayFrame()
{
  $webUrl = 'http://[external-web-domain.com]/';

  //Get HTML from the URL
  $content = file_get_contents($webUrl);

  //Add custom JS to returned HTML content
  $customJS = "
  <script>

      /* Here I am writing a sample jQuery to hide the navigation menu
         You can write your own jQuery for this content
      */
    //Hide Navigation bar
    jQuery(\".navbar\").hide();

  </script>";

  //Append Custom JS with HTML
  $html = $content . $customJS;

  //Return customized HTML
  return $html;
}

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.

<div id='myframe'>

  <?php 
   /* 
    Use below function to display final HTML inside this div
   */

   //Display Frame
   echo displayFrame(); 
  ?>

</div>

<?php

/* 
  Function to display frame from another domain 
*/

function displayFrame()
{
  $webUrl = 'http://[external-web-domain.com]/';

  //Get HTML from the URL
  $content = file_get_contents($webUrl);

  //Add custom JS to returned HTML content
  $customJS = "
  <script>

      /* Here I am writing a sample jQuery to hide the navigation menu
         You can write your own jQuery for this content
      */
    //Hide Navigation bar
    jQuery(\".navbar\").hide();

  </script>";

  //Append Custom JS with HTML
  $html = $content . $customJS;

  //Return customized HTML
  return $html;
}
梓梦 2024-07-13 22:38:00
document.getElementById('iframe01').outerHTML
document.getElementById('iframe01').outerHTML
巴黎盛开的樱花 2024-07-13 22:38:00

如果您在 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

久伴你 2024-07-13 22:38:00

您可以使用此代码从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文