2009-10-15 11 views

risposta

2

A meno che la pagina web è in iframe è dello stesso dominio della pagina che contiene, è impossibile.

Se hanno lo stesso dominio, quindi provare il seguente:

document.title = document.getElementById("iframe").documentElement.title; 
+1

'contentDocument'. Tranne in IE6-7, dove devi tornare al 'contentWindow.document 'non standard. 'documentElement' è il tag html root; la proprietà 'title' si trova sullo stesso HTMLDocument. – bobince

3

concessione che iframe src e lo src documento principale sono lo stesso dominio:

documento principale:

<html> 
<head><title>Parent</title> 
<body> 
    <iframe id="f" src="someurl.html"></iframe> 
    <script> 
     //if the parent should set the title, here it is 
     document.title = document.getElementById('f').contentWindow.document.title; 
    </script> 
</body> 
</html> 

someurl.html:

<html> 
<head><title>Child</title> 
<body> 
    <script> 
     //if the child wants to set the parent title, here it is 
     parent.document.title = document.title; 
     //or top.document.title = document.title; 

    </script> 
</body> 
</html> 
+1

iframe.contentWindow.document restituisce null in Chrome. – msbg

7

Se si vuole farlo utilizzando jQuery:

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

Puoi farlo solo quando le pagine sono sullo stesso dominio, altrimenti è inutile.

1

Un modo è possibile condividere il titolo e posizione:

document.write('<iframe src="http://www.yourwebsite.com/home.html?title='+document.title+'&url='+window.location+'" frameborder="0" scrolling="no"></iframe>'); 

e quindi è possibile leggere i parametri a pagina home.html.

0

Questo può essere fatto utilizzando i listener di eventi sulla pagina. Non è particolarmente elegante, ma i browser l'ho provato con il supporto (quindi IE9 +, Firefox, Chrome).

Nella pagina principale del sito aggiungere il seguente javascript:

function setPageTitle(event) { 
    var newPageTitle = event.data 
    // your code for setting the page title and anything else you're doing 
} 
addEventListener('message', setPageTitle, false); 

Nel iFrame, avrete quindi bisogno di avere il seguente script:

var targetOrigin = "http://your.domain.com"; // needed to let the browser think the message came from your actual domain 
parent.postMessage("New page title", targetOrigin); // this will trigger the message listener in the parent window 
Problemi correlati