最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting the base tag of a dynamically created iframe - Stack Overflow

programmeradmin3浏览0评论

I'm trying to dynamically create an iframe and set it's base tag before it is created.

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="/" />
var bt = ifrm.contentDocument.createElement("base");
bt.setAttribute("href", "/");
ifrm.contentDocument.getElementsByTagName("head")[0].appendChild(bt);

document.body.appendChild(ifrm);

I know i can set the base tag in the iframes src file itself. But I need to set it here. Thanks.

I'm trying to dynamically create an iframe and set it's base tag before it is created.

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./MyFolder/" />
var bt = ifrm.contentDocument.createElement("base");
bt.setAttribute("href", "http://www.example./Folder/");
ifrm.contentDocument.getElementsByTagName("head")[0].appendChild(bt);

document.body.appendChild(ifrm);

I know i can set the base tag in the iframes src file itself. But I need to set it here. Thanks.

Share Improve this question edited Oct 24, 2011 at 0:39 aquawicket asked Oct 24, 2011 at 0:08 aquawicketaquawicket 5841 gold badge10 silver badges27 bronze badges 1
  • I am still unable to get this working. Not sure where to go from here. – aquawicket Commented Oct 24, 2011 at 15:34
Add a ment  | 

1 Answer 1

Reset to default 4

You can append a <base> element to ifrm.contentDocument.getElementsByTagName("head").

You'll need to create it in the child document by calling ifrm.contentDocument.createElement("base")

For example:

var bt = ifrm.contentDocument.createElement("base");
bt.setAttribute(...);
ifrm.contentDocument.getElementsByTagName("head")[0].appendChild(bt);
发布评论

评论列表(0)

  1. 暂无评论