Angular JS provides clean method for closing browser window.
$window.close()
Seemingly valuable under the hood actually it only wraps the window object with angular service:
function $WindowProvider() {
this.$get = valueFn(window);
}
https://github.com/angular/angular.js/blob/master/src/ng/window.js
Few years ago there've been a security update of web browsers and now closing windows not opened by a script is forbidden, so execution of the $window.close()
method in Chrome or Firefox throws the exception (Scripts may close only the windows that were opened by it.
)
Here's great SO post describing it: https://stackoverflow.com/a/19768082/3775079
Solution provided by community is to open the same window and then close it:
var popup = window.open(location, '_self', '');
popup.close();
Unfortunately this solution applied to angular code doesn't work to me:
foo.controller('barCtrl',
function barCtrl($scope, ) {
var initialize = function () {
var popup = $window.open(location, '_self', '');
popup.close();
}
initialize();
}
);
It causes an infinite loop of window refresh.
How can I properly execute window.close method after security update in Angular solutions?