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
3 Answers
Reset to default 6Open 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
版权声明:本文标题:javascript - Chrome app closing window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741216273a2360103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论