Setting the base tag of a dynamically created iframe

别等时光非礼了梦想. 提交于 2020-01-04 04:44:07

问题


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.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);

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


回答1:


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);


来源:https://stackoverflow.com/questions/7870237/setting-the-base-tag-of-a-dynamically-created-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!