2016-12-29 59 views
0

我想從iframe中頁面的標題動態設置我的主(父)頁面的標題。如何從iframe中檢索和設置頁面的標題

例如,我有兩個頁面,Index.html和frame.html。

現在的index.html是具有連接到frame.html像這樣的iframe:

<iframe allowtransparency=true scrolling="auto" frameborder="0" src="frame.html"></iframe> 

frame.html是有代碼:

<head> 
<title> Main Title </title> 
</head> 
<body> 
body text 
</body> 

現在我想的標題可從frame.html的<title>標記(即「主標題」)中檢索Index.html。

有人可以告訴我我該怎麼做到這一點?

+0

執行父'window'和''iframe' window'具有相同的起源? – guest271314

回答

0

這應做到:

<iframe allowtransparency=true scrolling="auto" id="my-iframe" frameborder="0" src="frame.html"></iframe> 
<script> 
    $("#my-iframe").on('load',function() { 
     document.title = document.getElementById("my-iframe").contentDocument.title; 
    }); 
</script> 
+0

抱歉它沒有工作。 index.html的標題仍未出現。我只是複製了您在index.html中發佈的相同代碼。我究竟做錯了什麼? –

+1

你在你的index.html文件中加載了jQuery庫嗎?如果是這樣,它需要在我發佈的代碼上加載。 – HaukurHaf

+0

是的,現在它加入jquery後工作。謝謝。 –