Search code examples
javascriptjquerycodeigniter-3

go to previous window automatically after printing dialog closes


I have a page where the html elements reside and print function is called via javascript at the end.

<script type="text/javascript">
$(document).ready(function(){
    window.print();
    window.history.back();
})
</script>

now I have this code to redirect user after print or if cancel is clicked. it works on local but in live server it goes to the previous page immediately.


Solution

  • Use this to go back to previous page

    $(document).ready(function() {
       window.print();
       history.go(-1); 
    });
    

    or:

    $(document).ready(function() {
       window.print();
       history.back(); 
    });