2013-02-18 6 views
7

Sto testando su this page e non sono sicuro di cosa mi manchi.Accesso all'oggetto documento di una cornice con JavaScript

// Two frames on the page 
> document.getElementsByTagName("frame").length 
2 

// Same domain, so no security restrictions 
> document.getElementsByTagName("frame")[0].src 
"http://www.quackit.com/html/templates/frames/menu_1.html" 
> window.location.href 
"http://www.quackit.com/html/templates/frames/frames_example_1.html" 

// Can't access the document 
> document.getElementsByTagName("frame")[0].document 
undefined 

Sembra che questo dovrebbe funzionare, quindi qual è il problema? Deve funzionare in IE8, ma sto anche testando su Chrome (la nuova stabile).

+3

In realtà stai utilizzando i frame nel 2013 o sono quegli iFrame? – adeneo

+1

E riguardo 'document.getElementsByTagName (" frame ") [0] .contentDocument'? –

+0

'var frame = document.getElementsByTagName (" frame ") [0]; var frame_doc = frame.contentWindow.document || frame.contentDocument; '- quindi usa' frame_doc' come documento del frame – Ian

risposta

22

Il modo all-around per ottenere il contenuto di un telaio è con qualcosa di simile:

var theFrame = document.getElementsByTagName("frame")[0]; 
var theFrameDocument = theFrame.contentDocument || theFrame.contentWindow.document; 
var button = theFrameDocument.getElementById("mybutton"); 

Tuttavia, è possibile ottenere il documento 's un <frame> utilizzando il suo nome , come:

window.frames["frame_name"].document 

se il codice HTML sono stati:

<frame name="frame_name">...</frame> 
0

Potreste usare

parent.frame.location.href = ... 

Dove telaio è il nome/id del fotogramma si d da modificare.

saluta Marc

Problemi correlati