0

Please excuse my ignorance but I am new in angularjs, I want to display a popup that has 2 buttons (OK and Cancel) OK : close the popup and the browser Cancel : close the popup

I am using modal-dialog to display the popup My html code is :

<modal-dialog show='dialogShown' dialog-title='My Dialog'>
          <p></p>
          <p>Are you sure ?</p>
          <button ng-click="closeWindow()">OK</button>
          <button ng-click="closeDialog()">Cancel</button>

</modal-dialog>

My controller :

var app = angular.module('MyModule', []);
app.controller('MyCtrl', function ($scope,myservice) {

    $scope.dialogShown = false;
    $scope.showDialog = function(){
        $scope.dialogShown = true;
    }

    $scope.closeWindow = function(){
        // I need to close the popup AND the window
    }

    $scope.closePopup = function(){
        // I need to close the popup 
    }    
}

Thanks in advance for your help.

  • by window you mean the browser tab? – Icycool Jun 29 '17 at 11:14
  • Yes I mean the browser tab – anonymouslyGeek Jun 29 '17 at 11:19
  • Have you created the modal-dialog directive ? is not a native html tag so you have to create the directive on your own like htis : http://adamalbrecht.com/2013/12/12/creating-a-simple-modal-dialog-directive-in-angular-js/ or try to use Bootstrap modals or Angular Material library – Alburkerk Jun 29 '17 at 11:48
  • Hello @Alburkerk, I tried the bootstrap modal, but I have the same issue, I can close the popup, but not the browser tab. Apparently it's impossible due to security purpose. – anonymouslyGeek Jun 29 '17 at 11:51
  • Oh you want to close the window totally my bad I didn't get it correctly. According to this link : https://stackoverflow.com/questions/14373625/close-current-tab it is not possible if you didn't create the tab from the website (only possible on past versions of IE because ... IE) – Alburkerk Jun 30 '17 at 01:19

1 Answers1

0

If you just need to be able to close the current tab of the browser, you should use the $window.close() function.

For more informations about this function, read the window.close() and the $window documentations.

EDIT : Like it's wrote in mozilla doc for window.close() :

This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

A workaround is to self (re)open the window then close it :

window.open('', '_self').close()

Angular way (with $window) :

$window.open('', '_self').close()

But this is a bug exploit and should not be used in production because it will probably be fixed in futur versions of browsers.

Vincent F
  • 27
  • 3