[英]`base` tag causes iframe to open as new window in Internet Explorer
我的base
标记有问题,它只影响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
标记并在创建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上测试过,效果很好!
正如评论中所述,target =“_ blank”使浏览器打开一个新窗口或选项卡(取决于您的配置)。
只需将其删除或自行替换(在IE8上测试):
<base target="_self"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.