javascript Chrome 应用程序关闭窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16578121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:12:03  来源:igfitidea点击:

Chrome app closing window

javascripthtmlgoogle-chromegoogle-chrome-app

提问by Bijx

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:

好的,所以我是一名初学者程序员,正在使用谷歌给我的模板来制作一个测试应用程序。我希望当有人单击文本按钮时,它会关闭窗口。该窗口使用 HTML 编码。我用了:

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

Please help

请帮忙

回答by Vincent Scheib

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

通过右键单击并选择“检查元素”,然后单击选项卡顶部列表中的“控制台”,在您的应用程序的开发人员工具中打开 Chrome 的 javascript 控制台。你会看见:

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.

拒绝执行 JavaScript URL,因为它违反了以下内容安全策略指令:“default-src 'self' chrome-extension-resource:”。请注意,'script-src' 未明确设置,因此使用 'default-src' 作为后备。

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.

在打包的应用程序中,由于更严格的内容安全策略,您无法在 HTML 文件中使用 javascript。您必须创建 HTML,然后从 javascript 文件中找到该元素并注册一个 onclick 处理程序。

For an example, see the Window Statesample packaged app, look at window.htmland window.js.

例如,请参阅Window State示例打包应用程序,查看window.htmlwindow.js

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

window.close()将在 javascript 文件中工作。不过,您应该了解打包的应用程序特定的应用程序窗口 api并查看那里的其他选项,例如chrome.app.window.current().fullscreen().

回答by wchargin

EDIT:As pointed out by Zibriin 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.

编辑:正如Zibri评论中指出的那样,这不再适用于 Linux 上的 Chrome 67(可能更早)。这个答案现在已经过时了。我为后人保留原文。



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

谷歌浏览器通常不允许您使用window.closeJavaScript关闭窗口。

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.close();你的地址栏并按Enter,什么都不会发生,但是试试这个,它会起作用:

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

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

有关详细信息,请参阅window.closeJavaScript 在 Google Chrome 中不起作用的方法”

回答by Forest Katsch

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.

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

但我建议使用 WChargin 的答案,因为它现在可以工作。