Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
I have a simple HTML code to print the page. Below is the code:
<!DOCTYPE html>
<html>
<head>

<script>
function printPage()
{
    var w = window.open("http://www.sigmaaldrich.com/catalog/CofADocRequest.do?symbol=209104&LotNo=MKBP0842V&brandTest=SIGMA","_self");
    window.focus();
   window.print();
   }
</script>

</head>
<body >
<input type="button" onclick="printPage()" value="print a div!" />
</body>
</html>

What the code does is, it displays a button, on clicking that button it calls a function. The function uses open() to open a new URL in the same page by using the "_self " parameter.


As we can see in the code, the print() is being called after the call to open method. But in my browser IE11, the print pop is being shown befor loading the page.
Due to this I am not printing the correct page.

Can anybody help me on this.
Posted
Comments
Kornfeld Eliyahu Peter 23-Nov-14 8:57am    
window.open does not wait for the new page to load, only creates the new page...You should use the w object to wait for the page to load: w.addEventListener('load', w.print, true);
Sergey Alexandrovich Kryukov 24-Nov-14 2:15am    
That looks like a correct answer. You could easily check up to confirm it works and put it as a formal answer. Would you? :-)
—SA
SrikantSahu 24-Nov-14 2:05am    
function printPage()
{
var htmlContent="<html><head></head><body ><iframe style='width:800px;height:1200px;' src='http://www.sigmaaldrich.com/catalog/CofADocRequest.do?symbol=209104&LotNo=MKBP0842V&brandTest=SIGMA'></iframe> </body></html>";
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write(htmlContent);
newdocument.close();
}
[no name] 24-Nov-14 2:14am    
Post this as a solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900