Search code examples
javascriptjquerycross-browserweb-frontend

Cross Browser javascript window.open method


Hi i am currently working on a web app. I have to open a popup and print the page. After page printing process popup must be closed. In firefox everything its good but when it comes to Chrome its not working. it print empty page. Here is the code.

$.ajax({
    type: "POST",
    url:  BASE_URL + "blabalba.php",
    data: dataString,
    success: function(res){

        var data = JSON.parse(res);
        if(data.status == 1){
            window.open(BASE_URL + "fisBas?sale_id=" + data.lastInsertId + "&sale_type=" + data.sale_type + "", "_blank");
            location.reload();
        }
        else 
            swal(data.message, "", "error");

    },

    error: function(){
        swal("xxxx", "", "error");
    }

FisBas.php js code

$(function(){
  window.print();
  window.close(); 
 // this code works on firefox.
)};

Solution

  • I found the solution. Chrome needs a time delay to do this job. So here is update

    fisBas.php Js Code

        $(function(){
          window.print();
          setTimeout(function(){
            window.close();
          }, 1000)
        })
    

    window.open call

    window.open(BASE_URL + "fisBas?sale_id=" + data.lastInsertId + "&sale_type=" + data.sale_type + "", "_blank");
    setTimeout(function(){
       location.reload();
    }, 1000);