0

I read that if a user clicks a link, then you can use JavaScript to open it and it will not be blocked by a popup blocker. You can see that here:

Allow window.open to open new window and not popup

So, in angular, I created this directive:

.directive('kdExport', function () {

    return {
        restrict: 'A',
        scope: {
            options: '=kdExport'
        },
        controller: 'ExportImageController',
        link: function (scope, element, attrs, controller) {

            // Bind to the onclick event of our button
            element.bind('click', function (e) {

                // Prevent the default action
                e.preventDefault();

                // Copy our options
                angular.extend(controller.options, scope.options);

                // Generate the image
                controller.generateImage(function (preview) {

                    // Create our url
                    var url = '/kits/preview/' + preview.id;

                    // Open a new window
                    window.open(url, '_blank');
                });
            });
        }
    };
})

The problem is that it gets blocked in IE. How can I prevent IE from blocking this link?

Community
  • 1
  • 1
r3plica
  • 13,017
  • 23
  • 128
  • 290

1 Answers1

0

Opening a popup after a user interaction (the click) must be done just after the event.

Here, you perform the window.open in a callback function in controller.generateImage.

Just try to move your code after your angular.extend, and it'll works perfectly.

[...]
link: function (scope, element, attrs, controller) {

        // Bind to the onclick event of our button
        element.bind('click', function (e) {

            // Prevent the default action
            e.preventDefault();

            // Copy our options
            angular.extend(controller.options, scope.options);

            // Open a new window here, not in a callback function
            window.open(url, '_blank');
        });
    }
[...]
Tmb
  • 450
  • 12
  • 20
  • Ok, so how can I get the window to open after my code has executed (generateImage has promises in it). – r3plica Nov 10 '15 at 18:17
  • @r3plica You just can't do this in a promise, because it's not just after a user action – Tmb Nov 13 '15 at 15:53