javascript location.href onchange 事件监听器?

发布于 2024-09-29 14:22:46 字数 780 浏览 4 评论 0原文

我想在你离开页面时显示一条消息(不是一个烦人的警报,只是一些告诉你等待的html),在考虑它时我面临着某些困难:

  • 当用户在 浏览器,取消导航离开 行动,我想要发送消息 离开。

  • 每当单击任何链接时,都会出现该消息。

  • 当单击的链接刚刚打开另一个选项卡(忽略_blank目标)时,它不应该捕获

,它不应该捕获,即触发事件非常简单,问题是

$(document).unload(function()
{
    // display message
});

如果用户取消,消息不会消失。

一个可能的解决方法是:

$(window).unload(function()
{
    // display message

    setTimeout(function()
    {
        // hide message
    },5000);
});

但我想知道是否有更干净的方法,即当用户取消导航(或由于任何其他原因失败)时,我可以隐藏该消息。

编辑#2:

我刚刚注意到,使用上面的代码,在 FF 中,直到离开页面才会显示消息,此时如果用户按“停止”,他将收到 about:blank。如果他在此之前按“停止”,则永远不会显示该消息。这正是我想要的。

在 Internet Explorer 中,该消息永远不会显示,我假设这是因为 IE 处理内容的方式不同。我想知道 chrome 中会发生什么?

I want to show a message whenever you are leaving the page (not an annoying alert, just some html telling you to wait), in thinking about it I'm facing certain difficulties:

  • when the user presses Stop in the
    browser, cancelling the navigate-away
    action, I'd like the message to go
    away.

  • whenever any link is clicked, the message should appear.

  • it shouldn't capture when the clicked link just opens another tab ( ignore _blank target )

that being said, firing the event is pretty simple, with just something like

$(document).unload(function()
{
    // display message
});

the problem being that if the user cancels, the message wouldn't go away.

a possible fix would be:

$(window).unload(function()
{
    // display message

    setTimeout(function()
    {
        // hide message
    },5000);
});

but I wanted to know if there was a cleaner way, that just when the user cancels the navigation (or it fails for any other reason), I can hide the message.

Edit #2:

I just noticed that with the above code, in FF the message isn't displayed until the page is left, at which point if the user presses Stop, he will receive about:blank. If he presses Stop before that, then the message is never displayed. Which is exactly what I wanted.

In internet explorer the message is never displayed, I'm assuming that's because IE handles stuff differently. I wonder what happens in chrome?

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

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

发布评论

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

评论(2

乖乖公主 2024-10-06 14:22:46

对于第一点:

当用户在浏览器中按“停止”取消导航离开操作时,我希望该消息消失。

不久前我有同样的问题,并且得到了响亮的回应 -我随后的研究也支持了这一点——这是不可能的。一旦开始请求新页面,就不可能以编程方式可靠地“返回”。暂停确实可能是唯一的出路。

不过,其他两点应该相对简单:遍历每个链接(例如使用 jQuery),并添加一个打开确认窗口的 click 事件,并返回 false这样原始的 href 就不会打开。仅针对内部链接执行此操作应该相对容易(检查 target 属性,如果它是 _blank,则保留该链接。)

这可能会变得很难处理不过,链接已经具有点击事件,以及导致不同页面(例如表单提交)的其他事件。

As to the first point:

when the user presses Stop in the browser, cancelling the navigate-away action, I'd like the message to go away.

I had the same question a while back, and the resounding response - also backed by my subsequent research - was that this is impossible. Once you start a request for a new page, it's impossible to reliably "come back" from it programmatically. A timeout may indeed be the only way to go.

The two other points, though, should be relatively straightforward: Walk through every link (e.g. using jQuery), and add a click event that opens the confirmation window, and returns false so that the original href isn't opened. It should also be relatively easy to do this for internal links only (check for the target property, if it's _blank, leave the link alone.)

It may become tough to deal with links that already have click events, though, and other events leading to a different page like form submissions.

总攻大人 2024-10-06 14:22:46

这是一个适用于所有浏览器的解决方案。它使用 document.readyState 属性,该属性适用于除早期版本 FireFox 之外的所有浏览器(适用于版本 3.6.8)。如果浏览器支持readyState属性,它将检查readyState是否已加载(浏览器将转到另一个页面)或已完成(或表明该页面不会转到任何地方的其他内容)。如果浏览器不支持readyState,那么它将默认为您的解决方案。

(function(){
    var hideMessage=document.readyState?function(){
        setTimeout(function(){
            if(document.readyState=='loading'){
                hideMessage();
            }else{
                //hide message
            }
        },500);
    }:function(){
        // hide message
    }
    function displayMessage(){
        // display message
    }
    window.onbeforeunload=function(){
        displayMessage();
        setTimeout(hideMessage,document.readyState?10:5000);
    };
}());

Here is a solution that works in all browsers. It uses the document.readyState attribute which works in all browsers except early versions FireFox (works in version 3.6.8). If the browser supports the readyState attribute it will check if the readyState is load (browser is going to another page) or is complete (or something else indicating that the page is not going anywhere). If the browser does not support the readyState then it will default to your solution.

(function(){
    var hideMessage=document.readyState?function(){
        setTimeout(function(){
            if(document.readyState=='loading'){
                hideMessage();
            }else{
                //hide message
            }
        },500);
    }:function(){
        // hide message
    }
    function displayMessage(){
        // display message
    }
    window.onbeforeunload=function(){
        displayMessage();
        setTimeout(hideMessage,document.readyState?10:5000);
    };
}());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文