Jump to content

Window.print() Is Not Working As Expected


Recommended Posts

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.

Link to comment
Share on other sites

Hi raghavendramanandi,

 

Please try to uncheck "Enable Protected Mode" in internet options.

Another advice is "Ensure you are using the default IE security zone settings (Security tab, click "Reset all zones to default").

Placing your window.open statement within a try/catch block will capture failures caused by popup blockers."

And this article, it seems, also describes the solution of the problem.

 

I hope, it helps.

Link to comment
Share on other sites

  • 1 year later...

Hi waleeed12,

 

I've edited code. Try it.

<script>
function printPage(){

var w = window.open("enter_URL","_self");

setTimeout(function() {
   window.focus();                                         
    window.print();
}, 1000);
    
}
</script>
<input onclick="printPage()" type="button" value="print a div!" />

Enter your URL instead of:   enter_URL

 

I hope this solution helps.

 

Link to comment
Share on other sites

  • 3 years later...
On 3/30/2016 at 7:14 AM, svitlanataran said:

Hi waleeed12,

 

I've edited code. Try it.


<script>
function printPage(){

var w = window.open("enter_URL","_self");

setTimeout(function() {
   window.focus();                                         
    window.print();
}, 1000);
    
}
</script>
<input onclick="printPage()" type="button" value="print a div!" />

Enter your URL instead of:   enter_URL

 

I hope this solution helps.

 

I am using the code above and try to print the content of a website. Unluckily, the website is loaded but the print pop up is not shown. Does chrome block this? Any work around? 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...