PDA

View Full Version : Print with Javascript and CSS problem?



writeman
03-30-2007, 09:25 AM
Hi all.
I have a button function to printe a dynamic generated page.
Code is below. But the css section in the code seems not work.
Could someone help me to debug it? Thanks.

function sheetprint(){
var aname = document.getElementById('name').value;
var asex = document.getElementById('sex').value;
str='';
str=str + '<style type="text/css" media="print"> ';
str=str + ' table td{ ';
str=str + ' border: 1px solid red; ';
str=str + ' } ';
str=str + '</style> ';
str=str + '<table> ';
str=str + ' <tr><td colspan="4">Company</td></tr>';
str=str + ' <tr><td colspan="4">Note</td></tr> ';
str=str + ' <tr> ';
str=str + ' <td>Name:</td> ';
str=str + ' <td>'+aname+'</td> ';
str=str + ' <td>Sex</td> ';
str=str + ' <td>'+asex+'</td> ';
str=str + ' </tr> ';
str=str + '</table> ';
document.body.innerHTML = str;
window.print();
window.history.go(0);
}

mburt
03-30-2007, 10:22 AM
function sheetprint(){
var aname = document.getElementById('name').value;
var asex = document.getElementById('sex').value;
str="'';
str=str + '<style type="text/css" media="print"> ';
str=str + ' table td{ ';
str=str + ' border: 1px solid red; ';
str=str + ' } ';
str=str + '</style> ';
str=str + '<table> ';
str=str + ' <tr><td colspan="4">Company</td></tr>';
str=str + ' <tr><td colspan="4">Note</td></tr> ';
str=str + ' <tr> ';
str=str + ' <td>Name:</td> ';
str=str + ' <td>'+aname+'</td> ';
str=str + ' <td>Sex</td> ';
str=str + ' <td>'+asex+'</td> ';
str=str + ' </tr> ';
str=str + '</table> ';
document.body.innerHTML = str; ';
window.print();
window.history.go(0);
}
You only had one " in the variable. Makes it open, so you have to use "" to close it.

writeman
04-02-2007, 01:01 AM
It seems have no matter with that. I wonder wheather I can use css in this way.

mburt
04-02-2007, 06:24 PM
Did you use an event handler to call "sheetprint"?
Also, I cleaned up the code a bit, as it was invalid.

function sheetprint(){
var aname = document.getElementById('name').value;
var asex = document.getElementById('sex').value;
var str='';
str=str + '<style type="text/css" media="print"> ';
str=str + ' table td{ ';
str=str + ' border: 1px solid red; ';
str=str + ' } ';
str=str + '</style> ';
str=str + '<table> ';
str=str + ' <tr><td colspan="4">Company</td></tr>';
str=str + ' <tr><td colspan="4">Note</td></tr> ';
str=str + ' <tr> ';
str=str + ' <td>Name:</td> ';
str=str + ' <td>'+aname+'</td> ';
str=str + ' <td>Sex</td> ';
str=str + ' <td>'+asex+'</td> ';
str=str + ' </tr> ';
str=str + '</table> ';
document.body.innerHTML = str;
window.print();
window.history.go(0);
}

writeman
04-03-2007, 12:32 AM
hi
I call the function this way.

<script type="text/javascript">
function sheetprint(){
var aname = 10;
var asex = '男';
var str='';
str=str + '<style type="text/css" media="print"> ';
str=str + ' table td{ ';
str=str + ' border: 1px solid red; ';
str=str + ' } ';
str=str + '</style> ';
str=str + '<table> ';
str=str + ' <tr><td colspan="4">Company</td></tr>';
str=str + ' <tr><td colspan="4">Note</td></tr> ';
str=str + ' <tr> ';
str=str + ' <td>Name:</td> ';
str=str + ' <td>'+aname+'</td> ';
str=str + ' <td>Sex</td> ';
str=str + ' <td>'+asex+'</td> ';
str=str + ' </tr> ';
str=str + '</table> ';
document.body.innerHTML = str;
window.print();
window.history.go(0);
}
</script>

<input type="button" onclick="javascript:sheetprint();" value="print a sheet">