then
cont_name = "";
cont_identifier = wrap_identifier+wrap_name+" div:eq("+i+")";
}
}
var num_repeat = 0;
var repeat_name = "";
repeat_name = $(cont_identifier+cont_name+" > div").eq(0).attr("class");
if(repeat_name == null){
num_repeat = $(cont_identifier+cont_name+" > div").length;
}else{
num_repeat = $(cont_identifier+cont_name+" > div."+repeat_name).length;
}
//////////////////////////////////////////////////////
// Styling each container, and repeat if appropriate.
var cont_p_width = 2+p_width;
var cont_p_length = 1+p_length;
// these dimensions structure the overflow. one unit on either side, one unit on top.
var cont_css = "{ overflow: hidden; width: "+cont_p_width+p_unit+"; ";
if(kind == "scroll"){
cont_css = cont_css + "position: absolute; ";
}else if(kind=="fixed"){
cont_css = cont_css + "height: "+cont_p_length+p_unit+"; position: fixed; ";
}
cont_css = cont_css + "top: -1"+p_unit+"; left: -1"+p_unit+"; } ";
var repeat_css = "{} ";
// content can go in here as user desires.
p_m = p_m + cont_identifier + cont_name + cont_css;
if(repeat_name == null){
p_m = p_m + cont_identifier+cont_name+" > div" + repeat_css;
}else{
p_m = p_m + "."+repeat_name+repeat_css;
// must be a class, not ID
}
}
//////////////////////////////////////////////////////
// end the containers loop. Style body & wrapper.
var wrap_css = "{";
if(no_wrap){
// let's wrap this shit
wrap_name = "printjswrapper";
wrap_identifier = "#";
var temp_prewrap = $("body").html();
var temp_postwrap = "
"+temp_prewrap+"
";
$("body").html(temp_postwrap);
}
wrap_css = wrap_css + "overflow: hidden; width: "+p_width+p_unit+"; ";
if(kind == "scroll"){
wrap_css = wrap_css + "";
// length can be full, go over multiple pages.
}else if(kind=="fixed"){
wrap_css = wrap_css + "height: "+p_length+p_unit+"; ";
}
wrap_css = wrap_css + "} ";
p_m = p_m + wrap_identifier+wrap_name+wrap_css;
p_m = p_m + "body{height: "+p_length+p_unit+"; width: "+p_width+p_unit+"; overflow: hidden;} ";
//////////////////////////////////////////////////////
// Finishes the @media print CSS
var css = $("style").html();
// when "screen" media left unspecified, @print just writes on top of the normal style
css = css + "@media print{" + p_m+"} ";
//css = css + p_m; // for testing purposes only
if($("style").html() == null){
// if no ");
}else{
$("style").html(css);
}
window.print();
});
To install, simply save the above in your directory as “print.js”. Then, put the following two lines into your html page:
print.js works best in SafariThis is because Google Chrome has limited built-in paper size options and Firefox applies obnoxious user agent styles..