【问题标题】:how to set event listener to back button in IE?如何将事件监听器设置为 IE 中的后退按钮?
【发布时间】:2019-03-20 18:13:05
【问题描述】:

我需要设置一个事件监听器来监听浏览器中的后退按钮。我需要在用户点击后退按钮后显示alert

【问题讨论】:

  • 请花几分钟时间阅读How to Ask。很难理解当前版本以及您想要的确切内容。话虽如此,后退按钮无法从窗口内部访问
  • 对不起,我编辑它^^
  • 嗯,你无法判断用户是如何试图离开页面的......只知道他们将通过卸载事件离开

标签: javascript jquery html internet-explorer


【解决方案1】:

感谢您的否定回复.. 这是我的第一个问题:\

我发现这个解决方案非常有用>>

<SCRIPT type="text/javascript">


                    window.onload = function () 
                    {
                        if (typeof history.pushState === "function") 
                        {
                            history.pushState("jibberish", null, null);
                            window.onpopstate = function () 
                            {
                                history.pushState('newjibberish', null, null);
                                // Handle the back (or forward) buttons here
                                // Will NOT handle refresh, use onbeforeunload for this.
                            };
                        }
                        else 
                        {
                            var ignoreHashChange = true;
                            window.onhashchange = function () 
                            {
                                if (!ignoreHashChange) 
                                {
                                    ignoreHashChange = true;
                                    window.location.hash = Math.random();
                                    // Detect and redirect change here
                                    // Works in older FF and IE9
                                    // * it does mess with your hash symbol (anchor?) pound sign
                                    // delimiter on the end of the URL
                                }
                                else 
                                {
                                     ignoreHashChange = false;   
                                }
                            };
                    }
                }


</SCRIPT> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-30
    • 2020-12-21
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多