如何在IE中将事件监听器设置为后退按钮?

时间:2016-11-20 15:05:50

标签: javascript jquery html internet-explorer

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

1 个答案:

答案 0 :(得分:0)

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

我找到了这个解决方案及其非常有用的>>

<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>