`base`标记会导致iframe在Internet Explorer中作为新窗口打开 [英] `base` tag causes iframe to open as new window in Internet Explorer
问题描述
我对 base
标签,它只影响Internet Explorer(版本8,9和10)。
I have a problem with the base
tag which only affects Internet Explorer (versions 8, 9 and 10).
以下代码用于在iframe中打开动态内容并且其功能正常在Chrome和Firefox中。它也可以在Internet Explorer中正常运行,但只能在< base target =_ blank/>
标签中使用。包含这个标签会导致iframe作为一个新窗口打开(这很有道理,但这不是我正在尝试做的事情。
The following code is used to open dynamic content in an iframe and it functions correctly in Chrome and Firefox. It also functions correctly in Internet Explorer, but only without the <base target="_blank"/>
tag. The inclusion of this tag causes the iframe to open as a new window (which makes sense, however this is not what I am trying to do.
<!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>
我该如何解决这个问题?不幸的是,我无法让代码在小提琴中工作,这可能是因为它依赖于< code>< base /> 头>
。
How can I fix this issue? Unfortunately, I couldn't get the code to work in a fiddle, perhaps because it relies on <base/>
being in the <head>
.
推荐答案
我刚刚删除了 /msie/.test (navigator.userAgent.toLowerCase())||
部分,并在IE8上正常工作。你确定你需要这段代码吗?
I just removed the /msie/.test(navigator.userAgent.toLowerCase()) ||
part, and works fine on IE8. Are you sure that you need this piece of code?
然而,不管你是否不想删除这段代码,你可以删除 base
标记并在 之后添加它
However, whether you don't want remove this piece, you can remove the base
tag and add it after you create the 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上测试过,效果非常好!
I tested on chrome, IE8, IE11 and works perfectly!
这篇关于`base`标记会导致iframe在Internet Explorer中作为新窗口打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!