我有一个问题base tag http://www.w3schools.com/tags/tag_base.asp仅影响 Internet Explorer(版本 8、9 和 10)。
以下代码用于在 iframe 中打开动态内容,并且它在 Chrome 和 Firefox 中可以正常运行。它在 Internet Explorer 中也能正常运行,但只是没有<base target="_blank"/>
标签。包含此标签会导致 iframe 作为新窗口打开(这是有道理的,但这不是我想要做的。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<base target="_blank"/>
</head>
<body>
<div id="main"></div>
<script type="text/javascript">
function load_iframe(name, height, width) {
var div = document.getElementById(name);
var ifrm = document.createElement('iframe');
ifrm.id = 'iframe_' + name;
ifrm.frameBorder = 0;
ifrm.scrolling = 'no';
ifrm.noresize = 'noresize';
ifrm.marginheight = 0;
ifrm.marginwidth = 0;
if (height !== 0) {
ifrm.height = height;
}
if (width !== 0) {
ifrm.width = width;
}
div.appendChild(ifrm);
content = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head></head><body></body></html>';
if (/msie/.test(navigator.userAgent.toLowerCase()) || window.opera) {
ifrm.contentWindow.contents = content;
return ifrm.src = 'javascript:window["contents"]';
} else {
doc = ifrm.contentDocument;
doc.open();
doc.write(content);
doc.close();
return ifrm;
}
}
load_iframe('main', 250, 300);
</script>
</body>
</html>
我该如何解决这个问题?不幸的是,我无法让代码正常工作,也许是因为它依赖于<base/>
处于<head>
.
我刚刚删除了/msie/.test(navigator.userAgent.toLowerCase()) ||
部分,并且在 IE8 上运行良好。您确定需要这段代码吗?
但是,无论您不想删除这一部分,都可以删除base
标记并添加它after你创建了iframe
:
load_iframe('main', 250, 300);
//and then:
var hd = document.getElementsByTagName("head")[0];
var bs = document.createElement("base");
bs.target= "_blank";
hd.appendChild(bs);
我在 chrome、IE8、IE11 上测试过,完美运行!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)