如何从iframe中加载的页面获取html标题?

时间:2022-12-04 23:35:23

This is sure a popular question.. But I did not post it without reading those.. well 80% of them doesn't have a valid answer.. just the talk about same origin policy.. Well My php pages r all in the same domain..

这肯定是一个流行的问题..但我没有阅读那些没有发布它...好吧80%没有一个有效的答案..只是关于相同的原始政策的谈论..好吧我的PHP页面全部在相同的域名..

So this is what I was getting in most of the places that had some logical answers..

所以这就是我在大多数有一些合乎逻辑的答案的地方所得到的......

    $(document).ready(function() {
       document.title=$("#mainFrame").contents().find("head").find('title').html();
    });

my iframe's id is "mainFrame".. I want to get the title of the page that loads inside the iframe & set that title as the parent's title everytime.

我的iframe的id是“mainFrame”..我想得到在iframe中加载的页面的标题,并且每次都将该标题设置为父标题。

So is there any efficient way to do that? this snippet doesn't work.. I tried.. it returns undefined!

那么有没有有效的方法呢?这个片段不起作用..我试过..它返回undefined!

2 个解决方案

#1


var title = $( "#frame_id").contents( ).find( "title").html( );
$( document).find( "title").html( title);

#2


You don't need jQuery for this. In your parent just do:

你不需要jQuery。在你的父母只做:

var iFrame = document.getElementById('mainFrame'), // Or $('#mainFrame')[0],
    iFrameDoc = iFrame.contentWindow.document || iFrame.contentDocument;

document.title = iFrameDoc.title;

contentDocument is the iframe's document.

contentDocument是iframe的文档。

#1


var title = $( "#frame_id").contents( ).find( "title").html( );
$( document).find( "title").html( title);

#2


You don't need jQuery for this. In your parent just do:

你不需要jQuery。在你的父母只做:

var iFrame = document.getElementById('mainFrame'), // Or $('#mainFrame')[0],
    iFrameDoc = iFrame.contentWindow.document || iFrame.contentDocument;

document.title = iFrameDoc.title;

contentDocument is the iframe's document.

contentDocument是iframe的文档。