0

I've looked at this question: js window.open then print() and it works just fine. My challenge is that I have a lot of styling that I would like to retain in the opened print window. I've tried changing

var printWindow = window.open('', '', 'width=1000,height=700');

to

var printWindow = window.open('print.aspx', '', 'width=1000,height=700');

and then using jQuery like this:

$("#print", printWindow.document).html($("#tabContainer").html());

but the window I get opened does not contain anything from the actual print.aspx file.

Can anyone shed some light on what's going wrong?

Edit: This is weird (at least to me)... My print.aspx has this:

<body >
    <div id="print">print content here...</div>
</body>

and from my calling page this works (returns 1):

alert(printWindow.document.getElementsByTagName('body').length);

but changing 'body' to 'div' makes it "fail" returning 0

Community
  • 1
  • 1
CJe
  • 1,928
  • 3
  • 24
  • 53

1 Answers1

0

You should provide the full url when possible, else it tries to use the relative url.

var printWindow = window.open('', '', 'width=1000,height=700'); would open a blank url

var printWindow = window.open('print.aspx', '', 'width=1000,height=700'); opens page print.aspx relative to current url. So if current url is www.google.com, it opens www.google.com/print.aspx, which would be 404 page not found.

Be careful of some sites which will redirect you, which can confuse you. For example if you run this on current stackoverflow page it would open this page itself because http://stackoverflow.com/questions/18633800/xxxxxx/print.aspx would redirect to http://stackoverflow.com/questions/18633800/xxxxxx

Also the jquery wont work because you cannot copy content from one window to another through javascript. Both are separate and cannot communicate.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • Wouldn't print.aspx be opened relatively to my current aspx page rather than the server? – CJe Sep 05 '13 at 11:10
  • See my second comment to OP – CJe Sep 05 '13 at 11:13
  • Yes it would open relative to the url. So if current url is xxxxx.aspx your new url would be xxxxx.aspx/print.aspx – user568109 Sep 05 '13 at 11:16
  • tried alert(printWindow.document.getElementById('print')); right after window.open - returns null even though my print.aspx contains
    print content here...
    in the body
    – CJe Sep 05 '13 at 11:16
  • Are you running this from original window ? – user568109 Sep 05 '13 at 11:20
  • everything is running on the same server. From a.aspx I open b.aspx located in the same directory. – CJe Sep 05 '13 at 11:21