mardi 29 janvier 2019

javascript - window.onload not working after opening a new blank window/page

What I am trying to do is a tool in which it will export a pdf file.

There are two cases:

  1. I don't have to check for anything and directly after the user clicks the export button it will open a new blank window because I am using headers to download the file and after the blank page is finished loading, it will close.

script for case 1

$('body').on("click", '.export-invoice-pdf', function () { // get id let id = $(this).closest('tr').data('id'); let newWindow = window.open( 'url' + id, "_blank" ); newWindow.onload = function() { newWindow.close(); }; });

This works fine


  1. I have to check if there is an available pdf file for the entry. before opening a new blank window to have the file and then close it afterwards just like in case1

script for case 2:

$('body').on("click", '.export-invoice-pdf', function () { // get id let id = $(this).closest('tr').data('id'); let newWindow = window.open( '', "_blank" ); //Ajax call to check if there is an available file $.ajax({ type: 'POST', url: 'url', data: {'orderId': orderId}, dataType: 'json', encode: true, }).success(function (data) { console.log('1'); if (data.hasFile === true) { //if has file do the samething as case 1 newWindow.location.href = 'url' + orderId; newWindow.onload = function() { newWindow.close(); }; } else { alert('no file to export'); } }).error(function () { }); });

Now this is not working. Actually my first problem was it's not making the new window because it treats it as a popup but I got that fixed after an hour of research. Now the closing of the window is my problem.

I have looked it up already in and still currently looking not just in here but sadly I was not able to find an answer about this problem. Any kind of help is appreciated.




Aucun commentaire:

Enregistrer un commentaire