使用javascript打印预览

时间:2012-04-18 10:21:03

标签: javascript asp.net css

这里我有用于打印和打印预览的工作代码,但它无法在 Mozilla中工作。请告诉我如何让它在mozilla中工作..i希望打印预览与irctc网站一样

<html>
<head>
<title>Print Preview</title>
<style type="text/css">
 @media print {
.noPrint {
    display:none;
  }
}
</style>
<script>
function printpr()
{
var OLECMDID = 7;
/* OLECMDID values:
* 6 – print
* 7 – print preview
* 1 – open window
* 4 – Save As
*/

var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);

WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";
}
</script>
</head>
<body>
<form runat="server">
<asp:radiobutton ID="Radiobutton1" class="noPrint" runat="server"></asp:radiobutton>
<asp:textbox ID="Textbox1" runat="server"></asp:textbox>
<asp:checkboxlist ID="Checkboxlist1" class="noPrint" runat="server">
    <asp:ListItem>gsdghsg</asp:ListItem>
    <asp:ListItem>ggsgh</asp:ListItem>
    <asp:ListItem Value="gdfgg">gfghghgf</asp:ListItem>
    <asp:ListItem></asp:ListItem>
    </asp:checkboxlist>
<div class="noPrint"><asp:textbox ID="Textbox2" runat="server"></asp:textbox></asp:checkboxlist></div>
<input type='button'class="noPrint" value="Print Preview" onclick="printpr();">
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

在mozilla中,最近具有一些id的项目不会自动暴露给全局命名空间(窗口对象)。

您需要定义一个变量:

var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);

var WebBrowser1 = document.getElementById('WebBrowser1');

WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";