Search code examples
async-awaitsafarimobile-safariwindow.openissue-tracking

Window.open() does not work on safari IOS 16


I am using JavaScript window.open() method to open new window inside of async function, it works well in IE, Firefox, chrome, brave but not working in safari for mobile.

fetch('http://example.com/movies.json')
  .then((response) => 
    if(response){
      const blob = new Blob([response], { type: "application/pdf" });
      const url = window.URL.createObjectURL(blob)
      window.open(url)
    }
  )
  .catch((error) => console.log(error));

Solution

  • I found one solution for above problem for all browsers with all OS (IOS, android and windows, linux). It's working for me.

    function downloadBlobFile(blob, fileName) {
        //Check the Browser type and download the File.
        const isIE = !!document.documentMode;
        if (isIE) {
            window.navigator.msSaveBlob(blob, fileName);
        } else {
            const url = window.URL || window.webkitURL;
            const link = url.createObjectURL(blob);
            const a = document.createElement("a");
            a.setAttribute("download", fileName);
            a.setAttribute("href", link);
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }
    }
    
    fetch('http://example.com/movies.json')
      .then((response) => 
        if(response){
          const blob = new Blob([response], { type: "application/octetstream" });
          downloadBlobFile(blob, "test.pdf")
        }
      )
      .catch((error) => console.log(error));