您的位置:首页 > Web前端 > JavaScript

JS动态生成的打印功能_winvay

2011-07-06 13:28 429 查看
 今天在XSLT里的JS脚本里,代码是为了在静态页面里增加IE里的“另存为”的按钮功能,写一个节点字符串,在JS里动态增加这个节点元素。

因为"<object></object>"在XSLT的模板里出现有错误,(该字符串里有单引号,可是形成的HTML后,成了双引号。另外,里面的尖括号,成了转义字符编码。导致不能在JS里填加节点)。
newInput.innerHTML = "<object id='WebBrowser' width=0 height=0 classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2object'>";
所以只能分开写:
var htmlstr="WebBrowser width=0 height=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2";
       newInput.innerHTML = "<object id="+htmlstr+"></object>";
另外,针对尖括号,在XSLT的文档里:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt">后增加<xsl:output method="text"/>就可以了。

最终代码如下:
Code
function addobject() {
    var TemO = document.getElementById("divOper");
       var newInput = document.createElement("div");
       var htmlstr="WebBrowser width=0 height=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2";
       newInput.innerHTML = "<object id="+htmlstr+"></object>";
       TemO.appendChild(newInput);
       document.all.WebBrowser.ExecWB(4, 1);  //另存为功能
      TemO.removeChild(newInput);   
}
附注:这个OBJECT对象,支持打印等多种IE操作:GOOGLE很多,我想写出在打印前后的编辑:
在静态页面中,点击“打印”后,一般不想打印出这个按钮,所以要加打印前后的事件代码:
代码 function window.onbeforeprint() {       //打印出来的效果  onbeforeprint
    document.getElementById("divOper").style.display = "none";
}
function window.onafterprint() {       //打印结束的页面效果 复原  onafterprint
    document.getElementById("divOper").style.display = "inline";

另外,在打印的时候,如果不想打印出页眉页脚,就可以在JS里增加以下代码:
代码 var HKEY_Root, HKEY_Path, HKEY_Key;
HKEY_Root = "HKEY_CURRENT_USER";
HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
function PageSetup_Null() {
try{
    var Wsh = new ActiveXObject("WScript.Shell");
    HKEY_Key = "header";
    Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");
    HKEY_Key = "footer";
    Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");
    }
    catch(err){}
    window.print();
}     这个在IE6里没有问题,但是在IE7就
(http://www.cnblogs.com/winvay/archive/2009/10/10/1580113.html )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息