如何获取 onbeforeunload 事件的目标 URL?

发布于 2024-08-10 00:40:04 字数 299 浏览 2 评论 0原文

我已经搜索了几个小时,但找不到解决方案。

window.onbeforeunload = warn;

这是行不通的:

function warn (e) 
{ 
   var destination = e.href;
   alert(destination );
}

好吧,所以要清理这些东西。如果用户单击页面本身上的链接,这很容易,因为您可以向所有链接的 onclick 事件添加事件处理程序,但是。我想捕获地址,即用户在浏览器的网址框中输入的内容。

I've searched for hours, but I couldn't find a solution for this.

window.onbeforeunload = warn;

This doesn't work:

function warn (e) 
{ 
   var destination = e.href;
   alert(destination );
}

Okay, so to clear the things. If the user clicks on a link on the page itself, it is easy, because you can add an eventhandler to all of the links onclick event, but. I want to catch the address, what the user types into the url box of the browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池木 2024-08-17 00:40:04

因为这是做不到的。新位置是私人/敏感信息。
没有人希望您知道他们离开您的网站后访问了哪些网站。

Because it can't be done. The new location is private/sensitive information.
Nobody wants you to know which sites they visit when they leave your site.

海拔太高太耀眼 2024-08-17 00:40:04

如果您只想查看链接目的地,可以使用 :

document.activeElement.href 

但获取地址行目的地是不可能的。

我听说过一些解决方案,如果鼠标移动到地址行,它们就会触发一个事件(警告用户还有未完成的进程尚未处理),但我永远不会这样做。

If you just want to see what link destination, you can use :

document.activeElement.href 

But getting the address line destination is not possible.

I've heard of solutions where they fire off an event if the mouse moves up to the address line (to warn the user that there are unfinished processes that have not been dealt with), but this sort of hack I would never do.

抱着落日 2024-08-17 00:40:04

Kaze 的答案是一种有趣的方法,但是当页面导航离开时查看元素焦点并不真正可靠。部分原因是链接单击和离开页面的导航之间存在延迟(在此期间用户可能会将焦点移动到其他元素,但也因为链接可能会获得焦点(例如通过键盘控制或鼠标按下) -click),但实际上并未用于离开页面,因此,如果您聚焦某个链接然后关闭窗口,它会认为您正在跟踪

该页面上的每个链接 。 (在每个表单上加上 onsubmit )稍微更可靠,但仍然可能由于延迟而被愚弄,例如您单击一个链接,但在新页面开始加载之前点击后退按钮(或。按 Esc 键)。再次,如果您关闭窗口,它会认为您正在点击该链接。

我想捕获地址,即用户在浏览器的网址框中输入的内容。

这种事不可能发生。这显然是隐私禁忌。

Kaze's answer is an interesting approach, but looking at the element focus when the page is navigated away from isn't really reliable. Partly because there is a delay between the link click and the navigation away from the page (during which time the user may move focus to some other element, but also because a link may be focused (eg. by keyboard control, or mousedown-without-click) without actually being used to navigate away from the page. So if you focused a link then closed the window, it'd think you were following the link.

Trapping onclick for every link on the page (plus onsubmit on every form) is slightly more reliable, but can still be fooled due to the delay. For example you click a link, but then before the new page starts loading hit the back button (or press Escape). Again, if you close the window it thinks you're following the link.

I want to catch the address, what the user types into the url box of the browser.

There is no way that will ever happen. It is an obvious privacy no-no.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文