2011-10-24 18 views
2

iframeを動的に作成し、その作成前にベースタグを設定しようとしています。動的に作成されたiframeのベースタグを設定する

ifrm = document.createElement("IFRAME"); 
ifrm.setAttribute("src", "test.html"); 
ifrm.style.width = 400+"px"; 
ifrm.style.height = 100+"px"; 

//before creating it, can we set he base tag? 
//I.E. <base href="http://example.com/MyFolder/" /> 
var bt = ifrm.contentDocument.createElement("base"); 
bt.setAttribute("href", "http://www.example.com/Folder/"); 
ifrm.contentDocument.getElementsByTagName("head")[0].appendChild(bt); 

document.body.appendChild(ifrm); 

iframe srcファイル自体にベースタグを設定できます。しかし、私はここにそれを設定する必要があります。おかげさまで

+0

私はまだこの作業を取得することができません。ここからどこに行くのかわからない。 – aquawicket

答えて

2

<base>要素をifrm.contentDocument.getElementsByTagName("head")に追加できます。

あなたは、例えばifrm.contentDocument.createElement("base")

を呼び出すことにより、子ドキュメントでそれを作成する必要があります:

var bt = ifrm.contentDocument.createElement("base"); 
bt.setAttribute(...); 
ifrm.contentDocument.getElementsByTagName("head")[0].appendChild(bt); 
+0

base = document.createElement( "base");これは正しい場所ですか? – aquawicket

+0

いいえ、あなたが子供の文書でそれを作成する必要があるので – SLaks

+0

私はまだalittle紛失しています、私は助けてくれる小さなサンプルコードを表示できますか? – aquawicket

関連する問題