2017年3月9日木曜日

idのないiframeのlocationをgetする方法

iframeのlocationプロパティの値を取得したい場合はiframeに対応するフレームエレメントをframesから取得することが必須ですが、document.body以下のiframeとframes以下のエレメントを紐付ける方法はid以外に見つからない、という記事を先日書きました。

ページ内に存在する既存のiframeについては紐付けは無理ですが、スクリプトから新規作成するものについては可能なことに気が付きました。
以下がそのサンプルです。

実行画面



以下ソース
<html>
<body>
<div id=div></div>
</body>
<script>
resizeTo(400,500)
onload = function(){
ifrD = document.createElement('iframe')
document.body.insertBefore(ifrD)
// createElementした時点ではframesの数は変化していない。
// insertBefore後にlengthが増える。
ifrF = frames[frames.length-1]
ifrD.onreadystatechange=function(){
div.innerText += '\n\n' + this.readyState + '\n' + ifrF.location
}
}
</script>
</html>

0 件のコメント:

コメントを投稿