Close the window when Chrome print dialog is clicked












1















I want to close the current tab in Chrome when the print button in Print Dialog is clicked by the user. I tried with window.print() and setTimeout(), but this will close the dialog even if the print is canceled by the CANCEL button.
Is there is any solution for this?



This is what I have done so far:



function printThis(){
window.print();
setTimeout(function(){
window.close();
},5000);
}









share|improve this question

























  • talk is cheap, show me the code.

    – Hiteshdua1
    Dec 29 '18 at 5:46











  • what i want is, when we click print button from print dialog (ctrl + p), the currently using tab should be closed.

    – Mohammed Sinan
    Dec 29 '18 at 5:49













  • Can you share the code you have tried ? I believe you need to stop that event from implementing that default behaviour , you would need to preventDefault() method.

    – Hiteshdua1
    Dec 29 '18 at 5:54











  • the code is shared above

    – Mohammed Sinan
    Dec 29 '18 at 5:56











  • Why don't you remove the window.print() command from the function? That way it wll close after 5 seconds after that function is invoked.

    – Hiteshdua1
    Dec 29 '18 at 5:58
















1















I want to close the current tab in Chrome when the print button in Print Dialog is clicked by the user. I tried with window.print() and setTimeout(), but this will close the dialog even if the print is canceled by the CANCEL button.
Is there is any solution for this?



This is what I have done so far:



function printThis(){
window.print();
setTimeout(function(){
window.close();
},5000);
}









share|improve this question

























  • talk is cheap, show me the code.

    – Hiteshdua1
    Dec 29 '18 at 5:46











  • what i want is, when we click print button from print dialog (ctrl + p), the currently using tab should be closed.

    – Mohammed Sinan
    Dec 29 '18 at 5:49













  • Can you share the code you have tried ? I believe you need to stop that event from implementing that default behaviour , you would need to preventDefault() method.

    – Hiteshdua1
    Dec 29 '18 at 5:54











  • the code is shared above

    – Mohammed Sinan
    Dec 29 '18 at 5:56











  • Why don't you remove the window.print() command from the function? That way it wll close after 5 seconds after that function is invoked.

    – Hiteshdua1
    Dec 29 '18 at 5:58














1












1








1


1






I want to close the current tab in Chrome when the print button in Print Dialog is clicked by the user. I tried with window.print() and setTimeout(), but this will close the dialog even if the print is canceled by the CANCEL button.
Is there is any solution for this?



This is what I have done so far:



function printThis(){
window.print();
setTimeout(function(){
window.close();
},5000);
}









share|improve this question
















I want to close the current tab in Chrome when the print button in Print Dialog is clicked by the user. I tried with window.print() and setTimeout(), but this will close the dialog even if the print is canceled by the CANCEL button.
Is there is any solution for this?



This is what I have done so far:



function printThis(){
window.print();
setTimeout(function(){
window.close();
},5000);
}






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 6:28









STaefi

3,42011635




3,42011635










asked Dec 29 '18 at 5:44









Mohammed SinanMohammed Sinan

62




62













  • talk is cheap, show me the code.

    – Hiteshdua1
    Dec 29 '18 at 5:46











  • what i want is, when we click print button from print dialog (ctrl + p), the currently using tab should be closed.

    – Mohammed Sinan
    Dec 29 '18 at 5:49













  • Can you share the code you have tried ? I believe you need to stop that event from implementing that default behaviour , you would need to preventDefault() method.

    – Hiteshdua1
    Dec 29 '18 at 5:54











  • the code is shared above

    – Mohammed Sinan
    Dec 29 '18 at 5:56











  • Why don't you remove the window.print() command from the function? That way it wll close after 5 seconds after that function is invoked.

    – Hiteshdua1
    Dec 29 '18 at 5:58



















  • talk is cheap, show me the code.

    – Hiteshdua1
    Dec 29 '18 at 5:46











  • what i want is, when we click print button from print dialog (ctrl + p), the currently using tab should be closed.

    – Mohammed Sinan
    Dec 29 '18 at 5:49













  • Can you share the code you have tried ? I believe you need to stop that event from implementing that default behaviour , you would need to preventDefault() method.

    – Hiteshdua1
    Dec 29 '18 at 5:54











  • the code is shared above

    – Mohammed Sinan
    Dec 29 '18 at 5:56











  • Why don't you remove the window.print() command from the function? That way it wll close after 5 seconds after that function is invoked.

    – Hiteshdua1
    Dec 29 '18 at 5:58

















talk is cheap, show me the code.

– Hiteshdua1
Dec 29 '18 at 5:46





talk is cheap, show me the code.

– Hiteshdua1
Dec 29 '18 at 5:46













what i want is, when we click print button from print dialog (ctrl + p), the currently using tab should be closed.

– Mohammed Sinan
Dec 29 '18 at 5:49







what i want is, when we click print button from print dialog (ctrl + p), the currently using tab should be closed.

– Mohammed Sinan
Dec 29 '18 at 5:49















Can you share the code you have tried ? I believe you need to stop that event from implementing that default behaviour , you would need to preventDefault() method.

– Hiteshdua1
Dec 29 '18 at 5:54





Can you share the code you have tried ? I believe you need to stop that event from implementing that default behaviour , you would need to preventDefault() method.

– Hiteshdua1
Dec 29 '18 at 5:54













the code is shared above

– Mohammed Sinan
Dec 29 '18 at 5:56





the code is shared above

– Mohammed Sinan
Dec 29 '18 at 5:56













Why don't you remove the window.print() command from the function? That way it wll close after 5 seconds after that function is invoked.

– Hiteshdua1
Dec 29 '18 at 5:58





Why don't you remove the window.print() command from the function? That way it wll close after 5 seconds after that function is invoked.

– Hiteshdua1
Dec 29 '18 at 5:58












3 Answers
3






active

oldest

votes


















1














please use this code.



window.addEventListener("beforeprint", function(event) { ... });
//or
window.onbeforeprint = function(event) { ... };


window.onbeforeprint will be called when press Ctrl+p or else for opening print dialog



e.g



window.addEventListener("beforeprint", 
function(event) {
window.close();
});


or if you want to close window after print button pressed, please use window.onafterprint like this



window.onafterprint = function(){
window.close()
}





share|improve this answer


























  • this will close if i click cancel button also. i want only to close after the print button is clicked

    – Mohammed Sinan
    Dec 29 '18 at 6:12











  • then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

    – Jin
    Dec 29 '18 at 6:14













  • even i checked with onafterprint . here problem is if i click cancel button.

    – Mohammed Sinan
    Dec 29 '18 at 6:31



















0














You can call the this method in onload, once this print dialog is dismissed the alert prompt will automatically be called and ask the user whether print or not if print has been done, the user will be automatically redirected to the page.



If you want to close the window you should open the particular window by window.open method, you cannot close the window by just opening by URL or redirects.



Example:



window.open("https://yourside.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");


Then you can call window.close in the window which is opened by window.open.



window.onload = function () {
window.print();
setTimeout(function(){ if(confirm("Have you printed ?")) { window.location.href="/your_page"; } }, 1);
}





share|improve this answer


























  • this wont work in my situation. here i can click print and click no for confirmation also.

    – Mohammed Sinan
    Dec 29 '18 at 6:28













  • Can you put your code

    – Googlian
    Jan 1 at 14:54



















0














You can use onafterprint event:



window.onafterprint = function(){ window.close() }





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967028%2fclose-the-window-when-chrome-print-dialog-is-clicked%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    please use this code.



    window.addEventListener("beforeprint", function(event) { ... });
    //or
    window.onbeforeprint = function(event) { ... };


    window.onbeforeprint will be called when press Ctrl+p or else for opening print dialog



    e.g



    window.addEventListener("beforeprint", 
    function(event) {
    window.close();
    });


    or if you want to close window after print button pressed, please use window.onafterprint like this



    window.onafterprint = function(){
    window.close()
    }





    share|improve this answer


























    • this will close if i click cancel button also. i want only to close after the print button is clicked

      – Mohammed Sinan
      Dec 29 '18 at 6:12











    • then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

      – Jin
      Dec 29 '18 at 6:14













    • even i checked with onafterprint . here problem is if i click cancel button.

      – Mohammed Sinan
      Dec 29 '18 at 6:31
















    1














    please use this code.



    window.addEventListener("beforeprint", function(event) { ... });
    //or
    window.onbeforeprint = function(event) { ... };


    window.onbeforeprint will be called when press Ctrl+p or else for opening print dialog



    e.g



    window.addEventListener("beforeprint", 
    function(event) {
    window.close();
    });


    or if you want to close window after print button pressed, please use window.onafterprint like this



    window.onafterprint = function(){
    window.close()
    }





    share|improve this answer


























    • this will close if i click cancel button also. i want only to close after the print button is clicked

      – Mohammed Sinan
      Dec 29 '18 at 6:12











    • then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

      – Jin
      Dec 29 '18 at 6:14













    • even i checked with onafterprint . here problem is if i click cancel button.

      – Mohammed Sinan
      Dec 29 '18 at 6:31














    1












    1








    1







    please use this code.



    window.addEventListener("beforeprint", function(event) { ... });
    //or
    window.onbeforeprint = function(event) { ... };


    window.onbeforeprint will be called when press Ctrl+p or else for opening print dialog



    e.g



    window.addEventListener("beforeprint", 
    function(event) {
    window.close();
    });


    or if you want to close window after print button pressed, please use window.onafterprint like this



    window.onafterprint = function(){
    window.close()
    }





    share|improve this answer















    please use this code.



    window.addEventListener("beforeprint", function(event) { ... });
    //or
    window.onbeforeprint = function(event) { ... };


    window.onbeforeprint will be called when press Ctrl+p or else for opening print dialog



    e.g



    window.addEventListener("beforeprint", 
    function(event) {
    window.close();
    });


    or if you want to close window after print button pressed, please use window.onafterprint like this



    window.onafterprint = function(){
    window.close()
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 29 '18 at 6:25









    NarendraR

    3,83161743




    3,83161743










    answered Dec 29 '18 at 5:58









    JinJin

    933322




    933322













    • this will close if i click cancel button also. i want only to close after the print button is clicked

      – Mohammed Sinan
      Dec 29 '18 at 6:12











    • then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

      – Jin
      Dec 29 '18 at 6:14













    • even i checked with onafterprint . here problem is if i click cancel button.

      – Mohammed Sinan
      Dec 29 '18 at 6:31



















    • this will close if i click cancel button also. i want only to close after the print button is clicked

      – Mohammed Sinan
      Dec 29 '18 at 6:12











    • then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

      – Jin
      Dec 29 '18 at 6:14













    • even i checked with onafterprint . here problem is if i click cancel button.

      – Mohammed Sinan
      Dec 29 '18 at 6:31

















    this will close if i click cancel button also. i want only to close after the print button is clicked

    – Mohammed Sinan
    Dec 29 '18 at 6:12





    this will close if i click cancel button also. i want only to close after the print button is clicked

    – Mohammed Sinan
    Dec 29 '18 at 6:12













    then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

    – Jin
    Dec 29 '18 at 6:14







    then you may use window.onafterprint event like this window.onafterprint = function(){ window.close() }

    – Jin
    Dec 29 '18 at 6:14















    even i checked with onafterprint . here problem is if i click cancel button.

    – Mohammed Sinan
    Dec 29 '18 at 6:31





    even i checked with onafterprint . here problem is if i click cancel button.

    – Mohammed Sinan
    Dec 29 '18 at 6:31













    0














    You can call the this method in onload, once this print dialog is dismissed the alert prompt will automatically be called and ask the user whether print or not if print has been done, the user will be automatically redirected to the page.



    If you want to close the window you should open the particular window by window.open method, you cannot close the window by just opening by URL or redirects.



    Example:



    window.open("https://yourside.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");


    Then you can call window.close in the window which is opened by window.open.



    window.onload = function () {
    window.print();
    setTimeout(function(){ if(confirm("Have you printed ?")) { window.location.href="/your_page"; } }, 1);
    }





    share|improve this answer


























    • this wont work in my situation. here i can click print and click no for confirmation also.

      – Mohammed Sinan
      Dec 29 '18 at 6:28













    • Can you put your code

      – Googlian
      Jan 1 at 14:54
















    0














    You can call the this method in onload, once this print dialog is dismissed the alert prompt will automatically be called and ask the user whether print or not if print has been done, the user will be automatically redirected to the page.



    If you want to close the window you should open the particular window by window.open method, you cannot close the window by just opening by URL or redirects.



    Example:



    window.open("https://yourside.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");


    Then you can call window.close in the window which is opened by window.open.



    window.onload = function () {
    window.print();
    setTimeout(function(){ if(confirm("Have you printed ?")) { window.location.href="/your_page"; } }, 1);
    }





    share|improve this answer


























    • this wont work in my situation. here i can click print and click no for confirmation also.

      – Mohammed Sinan
      Dec 29 '18 at 6:28













    • Can you put your code

      – Googlian
      Jan 1 at 14:54














    0












    0








    0







    You can call the this method in onload, once this print dialog is dismissed the alert prompt will automatically be called and ask the user whether print or not if print has been done, the user will be automatically redirected to the page.



    If you want to close the window you should open the particular window by window.open method, you cannot close the window by just opening by URL or redirects.



    Example:



    window.open("https://yourside.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");


    Then you can call window.close in the window which is opened by window.open.



    window.onload = function () {
    window.print();
    setTimeout(function(){ if(confirm("Have you printed ?")) { window.location.href="/your_page"; } }, 1);
    }





    share|improve this answer















    You can call the this method in onload, once this print dialog is dismissed the alert prompt will automatically be called and ask the user whether print or not if print has been done, the user will be automatically redirected to the page.



    If you want to close the window you should open the particular window by window.open method, you cannot close the window by just opening by URL or redirects.



    Example:



    window.open("https://yourside.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");


    Then you can call window.close in the window which is opened by window.open.



    window.onload = function () {
    window.print();
    setTimeout(function(){ if(confirm("Have you printed ?")) { window.location.href="/your_page"; } }, 1);
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 29 '18 at 6:19

























    answered Dec 29 '18 at 6:13









    GooglianGooglian

    639314




    639314













    • this wont work in my situation. here i can click print and click no for confirmation also.

      – Mohammed Sinan
      Dec 29 '18 at 6:28













    • Can you put your code

      – Googlian
      Jan 1 at 14:54



















    • this wont work in my situation. here i can click print and click no for confirmation also.

      – Mohammed Sinan
      Dec 29 '18 at 6:28













    • Can you put your code

      – Googlian
      Jan 1 at 14:54

















    this wont work in my situation. here i can click print and click no for confirmation also.

    – Mohammed Sinan
    Dec 29 '18 at 6:28







    this wont work in my situation. here i can click print and click no for confirmation also.

    – Mohammed Sinan
    Dec 29 '18 at 6:28















    Can you put your code

    – Googlian
    Jan 1 at 14:54





    Can you put your code

    – Googlian
    Jan 1 at 14:54











    0














    You can use onafterprint event:



    window.onafterprint = function(){ window.close() }





    share|improve this answer




























      0














      You can use onafterprint event:



      window.onafterprint = function(){ window.close() }





      share|improve this answer


























        0












        0








        0







        You can use onafterprint event:



        window.onafterprint = function(){ window.close() }





        share|improve this answer













        You can use onafterprint event:



        window.onafterprint = function(){ window.close() }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 29 '18 at 6:52







        user10845517





































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967028%2fclose-the-window-when-chrome-print-dialog-is-clicked%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'