zoukankan      html  css  js  c++  java
  • 页面打印URL的简单方法比较

    我们在编程种经常会碰到需要指定一个url地址,点击一个按钮,就能打印出这个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.fcsoft.com.cn/ePrint.htm" onclick="printURL(this.href); return false;">print http://www.fcsoft.com.cn/ePrint.htm</a>
    <p></p>
    <a target="_blank" href="http://www.fcsoft.com.cn" onclick="printURL(this.href); return false;">print http://www.fcsoft.com.cn/</a>
    </body>
    </html>

    上面这个示例点击链接就能打印,调用的是window.print();方法进行url打印。
    其实printURL()这个方法就是动态建立一行<LINK REL="alternate" media="print" href="..."/>执行打印url。任意页面指定一个<LINK REL="alternate" media="print" href="..."/> href后面链接的地址就是打印的地址。

    这种方法
    优点:不用打开页面,不用下载组件。
    缺点:不能预览,不能直接打印。

    再看一个利用插件打印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();   
        oPrintCtl.Preview();  //利用了插件的打印预览的功能  
        return true;
        }
      else return false;
      }
    </script>
    </head>
    <body>
    <object style="display:none" id="oPrintCtl" classid="clsid:CA03A5A8-9890-49BE-BA4A-8C524EB06441" codebase="eprintdemo.cab#Version=3,0,0,9" VIEWASTEXT></object> 
    <a target="_blank" href="http://www.fcsoft.com.cn/ePrint.htm" onclick="printURL(this.href); return false;">print http://www.fcsoft.com.cn/ePrint.htm</a>
    <p></p>
    <a target="_blank" href="http://www.fcsoft.com.cn" onclick="printURL(this.href); return false;">print http://www.fcsoft.com.cn/</a>
    </body>
    </html>


    上面这个示例点击链接进入打印预览,调用的是插件的oPrintCtl.Preview();方法进行url链接页面的预览。同样是用动态建立一行<LINK REL="alternate" media="print" href="..."/>执行打印url。但是利用了插件可以进行预览,打印,直接打印,指定打印参数等等,插件有很多属性和参数设置功能。

    有兴趣继续讨论的可以加msn:eprint01@hotmail.com
    QQ:6460267 “web打印”敲门

  • 相关阅读:
    【uoj3】 NOI2014—魔法森林
    【bzoj2002】 Hnoi2010—Bounce 弹飞绵羊
    【hdu4010】 Query on The Trees
    【uoj129】 NOI2015—寿司晚宴
    【bzoj2877】 Noi2012—魔幻棋盘
    【bzoj2876】 Noi2012—骑行川藏
    【bzoj2875】 Noi2012—随机数生成器
    【codeforces 235E】 Number Challenge
    【bzoj2154】 Crash的数字表格
    【bzoj3529】 Sdoi2014—数表
  • 原文地址:https://www.cnblogs.com/webreport/p/726809.html
Copyright © 2011-2022 走看看