我有一个包含跨域iframe的页面。在iframe中加载的页面上是一个打印按钮,它运行一些javascript来打印一个名为printFrame的iframe,它位于页面上(在嵌套页面上,而不是在父页面上)。当我点击按钮时,在FF中它可以工作,但IE给了我一个错误:frames.printFrame为null或不是对象。我糊涂了。代码没有尝试访问父文档,为什么它不起作用?
当我尝试访问printframe时,代码在iframedoc.html的第9行崩溃了
父文档HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<iframe src="http://www.otherdomain.com/iframeDoc.html"/>
</body>
</html>
iframedoc.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function printContent(){
frames['printFrame'].focus();
var printFrameDiv = frames['printFrame'].document.getElementById("printDiv");
printFrameDiv.innerHTML = document.getElementById('printableContent').innerHTML;
frames['printFrame'].print();
window.focus();
}
</script>
</head>
<body>
<iframe id="printFrame" name="printFrame" src="/printFrame.html"></iframe>
<div id="mainContent">
<div id="printableContent">
My printable content is here
</div>
<div id="nonPrintableContent">
Content that I dont want to print is here
</div>
<a href="javascript:void(0)" onclick="printContent()">Print</a>
</body>
</html>