开发者

Printing with javascript problems.....div comes as a null string

开发者 https://www.devze.com 2023-04-08 13:05 出处:网络
I am using a javascript function to print a page.I keep getting the string as a null value and im not sure how.....here is the code.The div i have is called divSheet and its set to visible false to be

I am using a javascript function to print a page. I keep getting the string as a null value and im not sure how.....here is the code. The div i have is called divSheet and its set to visible false to begin...when you load information it creates a table in the divSheet and sets it to true. Any ideas why it says the strid in getPrint function is null? Thank you!

            <asp:ImageButton runat="server" ID="imageBtnPrint" Style="z-index: 100" ImageUrl="~/Images/printerIcon.gif"
                               OnClientClick="javascript:getPrint('divSheet')" ToolTip="Print" />


    function getPrint(strid)  
    {
        var pp = window.open();
        var prtContent = document.getElementById(strid);
        pp.document.writeln('<HTML><HEAD><title>Print Confirmation Sheet</title><LINK href=PrintStyle.css  type="text/css" rel="stylesheet">')
        pp.document.writeln('<LINK href=PrintStyle.css  type="text/css" rel="stylesheet开发者_运维技巧" media="print"><base target="_self"></HEAD>')
        pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">');
        pp.document.writeln('<form method="post">');  
        pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right><INPUT ID="PRINT" type="button" value="Print" onclick="javascript:location.reload(true);window.print();"><INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();"></TD></TR><TR><TD></TD></TR></TABLE>');
        pp.document.writeln(document.getElementById(strid).innerHTML);
        pp.document.writeln('</form></body></HTML>');

    }


I think that's because you are spawning a child window from your main HTML, and the child page cannot access parent's variables directly.

You could set a local variable in the parent HTML to your div's ID, and then from your JS function access it using: window.opener.<variable name>

Something like this: (warning : untested)

...
var divId;
function getPrint(strid)  
{ 
    divId = strid;
    var pp = window.open();
    var prtContent = document.getElementById(window.opener.divId);
    ...
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消