【问题标题】:Get title from IFrame document从 IFrame 文档中获取标题
【发布时间】:2014-02-23 15:13:15
【问题描述】:

这是我的代码:

<div id="main-content">
    <script>
        document.title = document.getElementById("main-content-iframe").contentDocument.title;
    </script>
    <iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0" onload="document.title=parent.frames['cframe'].document.title;">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
</div>

它不起作用,我做错了什么?

【问题讨论】:

  • Javascript 控制台中是否有任何错误消息?
  • 脚本标签中的js不在文档就绪调用中,所以它在iframe加载之前执行,因此该部分将无法正常运行。
  • 你看过这篇文章吗?可能有用get iframe page title from javascript using jquery
  • @PubliDesign 这显示了 jQuery 语法,但由于他的语法是正确的,它不会解决问题。他的问题是时机。
  • 那你在打电话之前打个招呼吗?基本上你在那里做什么。

标签: javascript html iframe title


【解决方案1】:

您需要检查 iframe 是否已加载。

<div id="main-content">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0" onload="document.title=parent.frames['cframe'].document.title;">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
<script>
    $( "iframe" ).on('load',function() {
        document.title = document.getElementById("main-content-iframe").contentDocument.title;
    });
</script>

【讨论】:

  • 它几乎没有“没用”,是吗?
  • @Kalle H. Väravas .. 提供了另一种方法.. 有时它是某些人的解决方案.. @KenSharp .. 投票..'因为 SO 认为它也是正确的! !
【解决方案2】:

您需要获取页面的document.title。当尝试访问 iframe 中的元素时,您需要首先获取 iframe(您已经这样做了),然后您可以像往常一样访问文档的任何属性(iframe)。

一般性回答

<iframe id="myFrame" src="https://www.example.com"></iframe>
<button onclick="getIframeTitle()">get iframe title</button>
function getIframeTitle() {
  // getting our iframe
  var iframe = document.getElementById("myFrame");
  // logging the iframe's title
  console.log(iframe.contentWindow.document.title);
}

具体到您的问题

<div id="main-content">
    <script>
        document.title = document.getElementById("main-content-iframe").contentDocument.document.title;
    </script>
    <iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0" onload="document.title=parent.frames['cframe'].document.title;">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
</div>

请参阅How TO - Get Iframe Elements 了解更多信息。

【讨论】:

  • @SuperJumpBros contentWindow 不返回文档,它只允许您访问 iframe 的文档。 contentWindow 返回 Window 对象,而不是文档。窗口是包含文档的内容,但您不能仅使用它来访问 iframe。
【解决方案3】:

试试代码或许能帮到你

HTML code:
<div id="main-content">   
    <iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
</div> 

jQuery code:
alert($('#main-content-iframe').contents().find("title").text());

注意:确保 home.html 来自与您的主页相同的域。

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 2011-12-25
    • 2011-04-29
    • 2012-01-12
    • 2011-06-17
    • 1970-01-01
    • 2012-11-14
    • 2011-11-26
    • 2013-04-17
    相关资源
    最近更新 更多