I am trying to convert a dynamically generated inline style background-image: url("xyz") into an img src="xyx" that will be opened in a new tab/window for print. I can pull in the url inline image into the img src but when clicking on the print button, 2 pages open up; a page with the url text and the image in the parent window. The pages constantly refresh also.
function to convert the url inline style to an image in an external window for print and the div id with the inline style BG and print button:
function printImg() {
var printWin = window.open();
var content = "<html>";
printWin.document.write( src = document.getElementById("lbImage").style.backgroundImage );
src = src.replace('url(','');
src = src.replace('")','');
content += document.write('<img src='+src+'" />');
printWin.document.close();
printWin.focus();
printWin.print(content);
printWin.close();
}
<div style="background-image: url("mysite.com/images/randomImg.png"); display: block;" id="lbImage">
<a href"#" type="button" onClick="printImg()" target="_blank">Print</a>
I have been plugging at this for a day now and seem to have hit a wall... any suggestions would be greatly appreciated.