javascript 是否可以在javascript中添加浏览器代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3735198/
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
Is it possible to add a browser code in javascript
提问by Nagarajan Ganesh
I would like to create a webpage with browser specific in javascript.
我想使用特定于 javascript 的浏览器创建一个网页。
For example:
例如:
can I use code like this in my coding part
我可以在我的编码部分使用这样的代码吗
chrome.tab.onRemoved.addListener()in my webpage.
chrome.tab.onRemoved.addListener()在我的网页中。
If it is possible please suggest me.
如果可能,请建议我。
回答by Nivas
What exactly are you tring to acheive?
你到底想要达到什么目标?
If you want to know when the tab in which your webpage is loaded is closed, then window.onunloadshould help you.
如果您想知道加载网页的选项卡何时关闭,那么window.onunload应该可以帮助您。
If you want to know when another webpage is closed, you cannot do this.
如果您想知道另一个网页何时关闭,则无法执行此操作。
UPDATE:
You said that you want to know when the user closes the browser or tab. This is not possible.
更新:
您说您想知道用户何时关闭浏览器或选项卡。这不可能。
But for your purpose (getting feedback), I think all you need is to differentiate whether the user is navigating to a link in your page, or whether the user is typing another URL(or by clicking a favorite).
但是为了您的目的(获得反馈),我认为您需要区分用户是导航到您页面中的链接,还是用户正在输入另一个 URL(或单击收藏夹)。
I think for your requirement, whether the user closes the browser, or whether he types another URL, is the same - the user is navigating away from your site, and at that time you say you want to collect feedback.
我认为对于您的要求,无论用户关闭浏览器还是键入另一个 URL,都是相同的 - 用户正在离开您的网站,而那时您说您想收集反馈。
This can be done in javascript.
这可以在javascript中完成。
For all the clicks in your page that might lead to a page refresh (hyperlinks, buttons,...), set a flag.
Inwindow.onunload, check whether this flag is set.
- If it is set, then the user has clicked a link in your page, do nothing.
- If the flag is not set then the user is navigating away, time to collect feedback.
对于页面中可能导致页面刷新的所有点击(超链接、按钮等),设置一个标志。
在 中window.onunload,检查是否设置了此标志。
- 如果设置了,那么用户点击了你页面中的链接,什么都不做。
- 如果未设置标志,则用户正在导航,是时候收集反馈了。
Let me know if this would work.
让我知道这是否有效。
PS: Note that popups/any distractions during window.unload can be very annoying. I understand that this probably is the requirements given to you. But if possible, try other mechanisms to collect (voluntary) feedback from the user.
PS:请注意,window.unload 期间的弹出窗口/任何干扰都会非常烦人。我了解这可能是给您的要求。但如果可能,请尝试其他机制来收集用户的(自愿)反馈。
回答by Quentin
No, you cannot access extension-specific APIs from webpages.
不可以,您无法从网页访问特定于扩展程序的 API。
回答by Jaco Pretorius
The Navigator object contains all information about the visitor's browser.
Navigator 对象包含有关访问者浏览器的所有信息。
https://developer.mozilla.org/en-US/docs/Web/API/Window.navigator
https://developer.mozilla.org/en-US/docs/Web/API/Window.navigator
I think this is pretty much the extent of what is possible in terms of interacting with the specific browser. You can't access other tabs (for security reasons) or tell when a tab is closed.
我认为这几乎是与特定浏览器交互的可能性范围。您无法访问其他选项卡(出于安全原因)或告诉选项卡何时关闭。
回答by Vasil Popov
You can use use the onbeforeunload event:
您可以使用 onbeforeunload 事件:
<html>
<head>
<script>
var exit = 1;
function handleClose()
{
if (exit)
{
alert("Closing");
}
}
</script>
</head>
<body onbeforeunload="handleClose()">
<a href="test.html" onclick="exit=0">Navigate to other page</a>
</body>
</html>

