打印URL页面
<html>
<head>
<title>打印URL示例</title>
<script>
function printURL(sHref) {
if(document.getElementById && document.all && sHref){
if(!self.oPrintElm){
var aHeads = document.getElementsByTagName('HEAD');
if(!aHeads || !aHeads.length)
return false;
if(!self.oPrintElm)
self.oPrintElm = document.createElement('LINK');
self.oPrintElm.rel = 'alternate';
self.oPrintElm.media = 'print';
aHeads[0].appendChild(self.oPrintElm);
}
self.oPrintElm.href = sHref;
self.focus();
self.print()
return true;
}
else return false;
}
</script>
</head>
<body>
<a target="_blank" href="http://www.baidu.com" onclick="printURL(this.href); return false;">打印 百度首页</a>
</body>
</html>
存在问题:
不支持打印预览,不支持frameset框架.
解决方案:
可用showModelessDialog或window.open()弹出框架内需打印的页面。在打印页面获取参数,根据参数判断是否需要打印。
如:
控制页面:
<script type="text/javascript">
function printPage() {
showModelessDialog(parent.frames.mainFrame.document.location.href+'&print=true','print','dialogWidth:' +document.body.offsetWidth+ ';dialogHeight:'+document.body.offsetHeight+';dialogLeft:0px;dialogTop:0px;help:yes;dialogHide:yes');
}
</script>
<img src="../Images/Button/print.png" onclick="return printPage()" />
需打印页面:
<script type="text/javascript">
var url=document.location.search;
if(url.indexOf("print")!=-1)
{
if(confirm('是否打印该页面?'))
{
printURL(this.location.href);
}else
{
window.close();
}
}
</script>
<head>
<title>打印URL示例</title>
<script>
function printURL(sHref) {
if(document.getElementById && document.all && sHref){
if(!self.oPrintElm){
var aHeads = document.getElementsByTagName('HEAD');
if(!aHeads || !aHeads.length)
return false;
if(!self.oPrintElm)
self.oPrintElm = document.createElement('LINK');
self.oPrintElm.rel = 'alternate';
self.oPrintElm.media = 'print';
aHeads[0].appendChild(self.oPrintElm);
}
self.oPrintElm.href = sHref;
self.focus();
self.print()
return true;
}
else return false;
}
</script>
</head>
<body>
<a target="_blank" href="http://www.baidu.com" onclick="printURL(this.href); return false;">打印 百度首页</a>
</body>
</html>
存在问题:
不支持打印预览,不支持frameset框架.
解决方案:
可用showModelessDialog或window.open()弹出框架内需打印的页面。在打印页面获取参数,根据参数判断是否需要打印。
如:
控制页面:
<script type="text/javascript">
function printPage() {
showModelessDialog(parent.frames.mainFrame.document.location.href+'&print=true','print','dialogWidth:' +document.body.offsetWidth+ ';dialogHeight:'+document.body.offsetHeight+';dialogLeft:0px;dialogTop:0px;help:yes;dialogHide:yes');
}
</script>
<img src="../Images/Button/print.png" onclick="return printPage()" />
需打印页面:
<script type="text/javascript">
var url=document.location.search;
if(url.indexOf("print")!=-1)
{
if(confirm('是否打印该页面?'))
{
printURL(this.location.href);
}else
{
window.close();
}
}
</script>