1
A
回答
2
打印IFrame的内容时有一些限制。 JavaScript安全策略会阻止您在其他域中打印某些内容。这是我多年来用来在页面上打印特定内容的方法。
首先你需要一个简单的JavaScript打印功能。这是我使用的一个
print = function(str) {
var WinPrint = window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.open("text/html", "replace");
WinPrint.document.write('<html><head><title>Printing</title></head><body>');
WinPrint.document.write(str);
WinPrint.document.write('</body></html>');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
};
然后,你需要从IFRAME
getIFrameContent = function(IFrameID) {
return document.getElementById(IFrameID).contentWindow.document.body.innerHTML;
};
然后一个简单的功能拉HTML来包装整个事情更容易
printIFrame = function(IFrameID) {
print(getIFrameContent(IFrameID));
};
一旦你拥有了所有可以调用printIFrame方法并简单地发送IFrame的ID的方法。方法2: 在新窗口中打开Iframe并打印该窗口。
var printIFrame2 = function (IFrameID) {
var URL = document.getElementById(IFrameID).getAttribute("src");
var WinPrint = window.open(URL, 'WinPrint', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.focus();
WinPrint.print();
WinPrint.close();
};
1
我不知道其他人发布的代码,除非我问一个问题。所以对于Cris来说,这是你要将JavaScript放在主页面中的吗?
<script type="text/javascript">
print = function (str) {
var WinPrint = Window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.open("");
WinPrint.document.write('');
WinPrint.document.write(str);
WinPrint.document.write('</body></html>');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
};
getIFrameContent = function (IFrameID) {
return document.getElementById(IFrameID).contentWindow.document.body.innerHTML;
};
printIFrame = function (IFrameID) {
print(getIFrameContent(IFrameID));
};
</script>
然后从后面的代码调用,像这样......
ScriptManager.RegisterStartupScript(Page, Me.GetType(), "key", "printIFrame(" & MyIFrame.ID & ");", True)
+0
如果您需要从服务器端调用打印iframe,这将起作用。另一种方法是在页面上添加一个按钮或超链接,调用脚本就像'Print IFrame' –
0
这是我测试:
页的打印部分:
<div id="printArea">
<h1>I am going to print from here</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
打印按钮:
<a href="#" onClick="printDiv();return false;">Print</a>
JavaScript代码:
function printDiv() {
var w = window.open('', '','width=auto,height=auto,resizeable,scrollbars');
w.document.write($("#printArea").html());
w.document.close();
javascript:w.print();
w.close();
return false;
}
相关问题
- 1. 在新窗口中打开asp.net页面
- 2. 刷新页面并打开新窗口
- 3. 打印页面的一部分javascript
- 4. 打印html页面的一部分
- 5. 在新窗口中打开添加打印页面功能
- 6. 在新窗口中打开页面
- 7. HTML:在新窗口中打开页面
- 8. 打开html页面的一部分
- 9. 打开窗口并浏览页面
- 10. asp.net mvc部分页面更新与asp.net aspx部分页面更新
- 11. ASP.NET打印页面
- 12. Asp.net打印页面
- 13. url.Action在ASP.NET MVC的新窗口中打开链接页面
- 14. 打开html页面打印
- 15. 避免在打印后加载新页面,下一页应该从上一页打印页面的剩余部分打印 - 点阵式打印机
- 16. 如何在新窗口中打开页面并发送数据?
- 17. 在弹出式窗口中打开Twitter分享页面
- 18. 如何打开一个页面到一个新的窗口并打印它
- 19. 如何在JavaScript中打印部分呈现的HTML页面?
- 20. 如何在javascript中打印页面的两个部分之一
- 21. 如何在新标签页/窗口中打开ASP页面?
- 22. 部分加载asp.net页面
- 23. 用分页符打印html页面
- 24. 在jQuery Colorbox中打开外部页面
- 25. 无需在新窗口中打开页面或使用JavaScript在新窗口中打开网页html元素值
- 26. 页面跳转在Eaby列表模板,其中打开网页的新部分
- 27. 打开一个新页面:新窗口与新标签页
- 28. Rave - 在页面底部打印乐队
- 29. 在UIWebView中打开html页面的一部分
- 30. 仅使用url打印网页并且不打开新窗口?
什么叫部分页面呢?如果在IFrame中加载“partial page”,则可以使用javascript:window.open('partialpage.aspx','Partial Page);以弹出新窗口 – Waqas
是编号打开新窗口并且不打开新窗口,打印iframe中的内容。 – TroyS