【发布时间】:2023-04-08 12:19:01
【问题描述】:
base tag 有问题,它只影响 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> 中。
【问题讨论】:
-
您是否尝试过为 iframe 赋予值为“_self”的“target”属性?
-
是的,我试过了。我不认为
target是 iframe 的有效属性 -
div = document.getElementById(div);和ifrm.id = 'iframe_' + div;没有意义,div这里是 HTMLElement,而不是字符串。同样iframes 没有noresize属性,它是用于frames。 -
哎呀,我已经修复了
div变量,这是错误的。 -
@JoshuaSpence 如果删除基本标签,iframe 在 Chrome 和 IE10/9/8 中可以正常工作。 "target=_blank" 的目的是在新窗口/标签中打开链接,所以如果你不希望这个不要包含它。
标签: javascript html internet-explorer iframe