3

I've been trying to get this work in my angular project, searching over the internet and it seems none of the possibles answers are helping me.

My console says:

Scripts may close only the windows that were opened by it.

Because I'm trying to close my current window with a button.

<button onclick="window.close();">Close window</button>

and of course the reasons is described at the console message. I can not close this window because this was not opened by javascript. At the first time my close window button was inside a ng-view and didn't work, after that I've moved my button to my index page outside the ng-view getting the same result :(

After those changes and other more that I've been found on internet, I've created a project from scratch, simple app, basically working just with the ng-app on index.html and a couple of controller that do nothing. AND this button works.

This works:

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">    
</head>
<body ng-controller="MyController">
    <button onclick="window.close();">Close window</button>
</body>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
    <script src="MyControllers.js"></script>
</html>

This is not working:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html lang="en" ng-app="myApp" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html lang="en" ng-app="myApp" class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>my web</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <my-nav-bar></my-nav-bar>
    <div ng-view></div>
    <button onclick="window.close();">Close window</button>

    <script src="libs/jquery.js"></script>
    <script src="libs/angular.js"></script>
    <script src="libs/myapp/myapp.js"></script>
    <script src="libs/myapp/mycontroller.js"></script>
    <script src="libs/myapp/mydirectives.js"></script>
</body>
</html>

It says scripts may close only the windows that were opened by javascript but can anyone makes me notice if there any diference between this two html? if it's not, How the first html is following this rule and the second one doesn't'?

napstercake
  • 1,815
  • 6
  • 32
  • 57
  • 2
    Both work when opened as local files and both DO NOT work when hosted by webserver – Kirill Slatin Aug 08 '15 at 18:45
  • 2
    possible duplicate of [window.close and self.close do not close the window in Chrome](http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome) – Kirill Slatin Aug 08 '15 at 18:45
  • @KirillSlatin you are absolutely right man. I've just figure it out. I'm gonna check the link in your other comment. – napstercake Aug 08 '15 at 18:49
  • 1
    found few links why you can't do it. A security violation. [link1][1] [1]:http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome [2]:http://stackoverflow.com/questions/12314729/close-windows-that-were-not-opened-by-script-using-javascript – Raj Aug 08 '15 at 19:51

0 Answers0