3
document.addEventListener("DOMContentLoaded", printFunction);
    function printFunction() {
      //setTimeout(function(){
        var file_name = ("{{article['title']}}".replace(/[^0-9a-zA-Z_?: .]/g, ""));
        document.title="Scroll - "+file_name;
        window.print();
        setTimeout(function(){window.close();}, 1);
      //}, 1);
    }

Hey I am trying to print a page by opening a new tab for printing, where Print Preview automatically opens and if user prints or cancel the page, it automatically closes. But when I am opening it on two tabs print preview is only getting loaded on one tab and on other tab print preview is not getting loaded. Is there any workaround for this.

UPDATE

I comment out the setTimeout method and now I am able to open print preview on every tab but when I am closing one print preview my all other tab is getting closed

I am opening my Print page by clicking on a link which is like this

a(href="/article/print/{{article['id']}}", target="_blank")

UPDATE

Right now I tried is using _new in place of _blank or tab name. But when I am clicking print from 4 different places 4 tab for different article is opening with print preview but canceling the print preview on one the tab is not getting closed till I am canceling the print preview on the first tab which is opened. Ajax call is also getting stopped.

Update

I have a website which is infinite scrollable. Meaning 1st article I am getting through Flask after that I am using Ajax call to get other articles if user scrolls a bit. Now I have print button on every article. When user is clicking on a certain print button and if I have used target = _blank a new tab will open with print preview on. Now if he will click again on the print button again a new tab will open with the print preview of that article. Now if he cancels or print the page, the tab should automatically close. But its happening only when user has open one tab. If he has open more than one tab by clicking on Print button then all the print preview tab is getting closed when he cancels the first tab that he has opened and if he cancels the second or third tab then no print preview tab is getting closed till he manage to cancel print on first tab.

For eg:

infiniteScroll.html

<html>
<ul>
 <li>
  <a href="print.html" target="_blank">print</a>
 </li>
 <li>
  <a href="print.html" target="_blank">print</a>
 </li>
 <li>
  <a href="print.html" target="_blank">print</a>
 </li></ul>
</html>

print.html

<html>
<body>
<p>sldfnksdfsdvncsjdnvjkndsc</p>
<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", printFunction);
    function printFunction() {
    //- setTimeout(function(){
      //- LazyX.load();
      var file_name = ("{{article['title']}}".replace(/[^0-9a-zA-Z_?: .]/g, ""));
      document.title="Scroll - "+file_name;
      window.print();
      setTimeout(function(){window.close();}, 1);
    //- }, 1);
  }
</script>
</body>
</html>

And Moreover my AJAX Call on website is not happening if Print preview tab is not closed.

A bit same problem Stack Question

UPDATE/PROBLEMS

  1. Open multiple tabs of print preview(BY CLICKING ON ALL 3 PRINT), then I am unable to AUTOMATICALY close the tab after printing or cancel the 2nd/3rd or 4th tab. But if I print or cancel the first opened tab from print I am able to close automatically.

  2. If I open multiple tabs of print preview, and if I cancel or print 1st tab all tabs are getting closed.

  3. If I open print preview before my AJAX call on infinite page. then I need to wait for the finish of printing page or cancel the print preview.link shared above.

Community
  • 1
  • 1
Rahul Shrivastava
  • 1,391
  • 3
  • 14
  • 38

0 Answers0