admin管理员组

文章数量:1277885

Ok so i am a beginner programmer and was using the template google gave me to make a test app. I wanted that whenn someone clicked a text button, that it would close the window. The window uses HTML coding. i used:

<a href="JavaScript:window.close()">CLOSE</a>

Please help

Ok so i am a beginner programmer and was using the template google gave me to make a test app. I wanted that whenn someone clicked a text button, that it would close the window. The window uses HTML coding. i used:

<a href="JavaScript:window.close()">CLOSE</a>

Please help

Share Improve this question edited May 16, 2013 at 2:43 Forest Katsch 1,5552 gold badges15 silver badges27 bronze badges asked May 16, 2013 at 2:35 BijxBijx 191 gold badge1 silver badge4 bronze badges 2
  • Is this a Chrome packaged app or a website? – Forest Katsch Commented May 16, 2013 at 2:39
  • 3 This question is tagged java. Please, for the love of all that is holy, recognize that java != javascript! See What's the difference between Java and JavaScript? for more details (specifically this answer). – wchargin Commented May 16, 2013 at 2:43
Add a ment  | 

3 Answers 3

Reset to default 6

Open Chrome's javascript console in the developer tools for your app by right clicking and selecting Inspect Element, then clicking on 'console' in the top list of tabs. You will see:

Refused to execute JavaScript URL because it violates the following Content Security Policy directive: "default-src 'self' chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

In a packaged app, due to the stricter Content Security Policy, you are not able to use javascript in an HTML file. You must create your HTML and then from a javascript file find the element and register an onclick handler.

For an example, see the Window State sample packaged app, look at window.html and window.js.

window.close() will work from a javascript file. Though, you should be aware of the packaged app specific app window api and see the other options there, such as chrome.app.window.current().fullscreen().

EDIT: As pointed out by Zibri in the ments, this no longer works as of Chrome 67 on Linux (and possibly earlier). This answer is now obsolete. I retain the original text for posterity.


Google Chrome often doesn't let you close the window with window.close in JavaScript.

A workaround is to use:

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

You can try it out right now: enter javascript:window.close(); into your address bar and press Enter, and nothing will happen, but try this and it'll work:

javascript:window.open('','_self','');window.close();

See "window.close method of JavaScript not working in Google Chrome" for more info.

There's a new way to close windows using the new method window.close() in the dev version of Chrome: https://developer.chrome./dev/apps/app.window.html#type-AppWindow.

But I'd remend using WChargin's answer as it will work right now.

本文标签: javascriptChrome app closing windowStack Overflow