Bijx
Bijx

Reputation: 19

Chrome app closing window

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

Upvotes: 1

Views: 11149

Answers (3)

wchargin
wchargin

Reputation: 16037

EDIT: As pointed out by Zibri in the comments, 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.

Upvotes: 4

Vincent Scheib
Vincent Scheib

Reputation: 18610

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().

Upvotes: 6

Forest Katsch
Forest Katsch

Reputation: 1555

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

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

Upvotes: 0

Related Questions