最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS window.close not working - Stack Overflow

programmeradmin0浏览0评论

I'm having trouble calling the window.close function, without getting the following error:

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

Here is how I have my app laid out:

HTML:

<html ng-app="mynewAPP" ng-controller="appCTRL">

....blahblah </html>

My Angular controls work fine, but when I call window.close, it is not working (at least the closing part... rest of app is fine).

app.controller("appCTRL" ,function( [injections]){
  $window.close();
}

How would I go about allowing AngularJS to close the window it is sitting on?

For example the below script would work fine: (basic JS)

<html>
<head>
<script type="text/javascript">
    function closeme(){
        window.close(); 
    }

    </script></head>

    <body onload="closeme()"></body>
</html>

For those that are wondering how I am opening the page, simply by visiting "mypage.html" on the browser. Nothing fancy.

I'm having trouble calling the window.close function, without getting the following error:

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

Here is how I have my app laid out:

HTML:

<html ng-app="mynewAPP" ng-controller="appCTRL">

....blahblah </html>

My Angular controls work fine, but when I call window.close, it is not working (at least the closing part... rest of app is fine).

app.controller("appCTRL" ,function( [injections]){
  $window.close();
}

How would I go about allowing AngularJS to close the window it is sitting on?

For example the below script would work fine: (basic JS)

<html>
<head>
<script type="text/javascript">
    function closeme(){
        window.close(); 
    }

    </script></head>

    <body onload="closeme()"></body>
</html>

For those that are wondering how I am opening the page, simply by visiting "mypage.html" on the browser. Nothing fancy.

Share Improve this question edited Dec 18, 2014 at 15:43 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Dec 18, 2014 at 15:37 NateNate 1,7704 gold badges25 silver badges41 bronze badges 4
  • 3 The important part here is how you've opened the window, as the error message says. – JJJ Commented Dec 18, 2014 at 15:39
  • 1 Just as a new browser (i.e by visiting mypage.html) – Nate Commented Dec 18, 2014 at 15:40
  • 1 Then you can't close the window programmatically. The "basic JS" version would also not do it. – JJJ Commented Dec 18, 2014 at 15:41
  • yes, this "basic js" does do it, as i have tested it.. in both IE and Chrome (FF doesnt close it) – Nate Commented Dec 18, 2014 at 15:42
Add a ment  | 

1 Answer 1

Reset to default 7

Due to security restriction you can only close windows with window.close that were opened by javascript.

Reference: MDN

发布评论

评论列表(0)

  1. 暂无评论