2

We have a problem when downloading a pdf document on a safari on MacOS Sierra. We get a dataUrl from a server and then use a $window.open with a createObjectURL to push the file to the browser. But all that doesn't actually matters. But here is a snippet to reproduce my problem:

myApp.controller('MyCtrl', function($scope, $window, $http) {       
    $scope.open = function() {
        var windowy = $window.open('', '_blank');
        console.log('1');
        console.log(windowy);
        $http.get('http://httpbin.org/delay/0').then(function(response) {
            var windowx = $window.open('', '_blank');
            console.log('2');
            console.log(windowx);
        });
    }
});

I created a JsFiddle to show the problem, the url is: http://jsfiddle.net/ADukg/9023/

Here is the log from chrome (mac) chrome console log

And here is the log from safari 10 (mac)

safari console log

Why is the window undefined on safari in $http.get(...).then(...)

Ben Croughs
  • 2,566
  • 1
  • 20
  • 30
  • Pop up blocker. – epascarello Jan 06 '17 at 21:19
  • why is the first window.open not blocked then ? – Ben Croughs Jan 06 '17 at 21:21
  • 1
    because you probably did a click action... – epascarello Jan 06 '17 at 21:21
  • Safari still allows `window.open` in response to user actions when pop-ups are blocked. This is probably implemented as "allow if called in the context of a user action event handler". – zneak Jan 06 '17 at 21:22
  • check the jsfiddle, both window.open happen after each other in the click of a button, so the popup blocker should also block the 1st one – Ben Croughs Jan 06 '17 at 21:23
  • 2
    No the ajax call is asynchronous so it is not attached to the click event. The delay is what detaches it from the click event. Same thing would happen if you put it inside of a setTimeout. – epascarello Jan 06 '17 at 21:25
  • 1
    Why use an Ajax call? Set the pop up with the url you are making an Ajax call to and set the proper headers to force a download. – epascarello Jan 06 '17 at 21:27
  • in our solution the ajax calls fetches the file as a blob from the server stateless and authenticated using a session_token in the request headers – Ben Croughs Jan 06 '17 at 21:29
  • so you can do the same thing with the pop up....makes no difference they are both get requests... The server does not know the difference. – epascarello Jan 06 '17 at 21:36
  • http://stackoverflow.com/questions/16799483/using-jquery-and-iframe-to-download-a-file – epascarello Jan 06 '17 at 21:42

1 Answers1

0

we solved our problem in a pragmatic way, maybe not the most elegant solution, but for now it will do

myApp.controller('MyCtrl', function($scope, $window, $http) {       
    $scope.open = function() {
        var window = $window.open('', '_blank');
        $http.get('http://httpbin.org/delay/0').then(function(response) {
            console.log(window);
        }, function() {
            window.close();
     });
    } 
});
Ben Croughs
  • 2,566
  • 1
  • 20
  • 30