web-dev-qa-db-ja.com

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

これはうまくいくようですが、何が問題なのですか? IE8で動作する必要がありますが、Chrome(最新の安定版))でもテストしています。

13
Brigand

フレームのコンテンツを取得するための総合的な方法は、次のようなものです。

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

ただし、次のような名前を使用して<frame>のドキュメントを取得することは可能です。

window.frames["frame_name"].document

hTMLが次の場合:

<frame name="frame_name">...</frame>
32
Ian

あなたは使うことができます

parent.frame.location.href = ...

ここで、frameは変更するフレームの名前/ IDです。

マーク・グリー

1
mooonli