简体   繁体   English

如何使用javascript捕获浏览器刷新事件

[英]How to capture browser refresh event using javascript

I need to navigate to a particular page/start page of my application when user clicks on refresh button in any other page of my application.I need javascript function for this.Can anyone suggest the best possible way. 当用户点击我的应用程序的任何其他页面中的刷新按钮时,我需要导航到我的应用程序的特定页面/起始页面。我需要javascript函数。任何人都可以建议最好的方法。 thanks. 谢谢。

The code i tried is 我试过的代码是

 window.onbeforeunload = function (evt) {
              if (evt) {
                  evt.stopPropagation();   
                  console.log("refreshed in appBootstrap");
                  console.log("current url is "+location.href)
                  window.location.href= "/scm/mgmt/case";
                  console.log("relocated")




                    }
               }

This does not redirect me to the intended page rather remains in the same page on refresh.I need to navigate to the specified url on page refresh each time. 这不会将我重定向到目标页面,而是在刷新时保留在同一页面中。我需要每次都在页面刷新时导航到指定的URL。

You can try a counter kind of thing. 你可以尝试一种反制品。 if counter is 2 than redirect to your required page. 如果计数器是2而不是重定向到您所需的页面。

or you can try document.referrer but some browser may not support this. 或者您可以尝试document.referrer但某些浏览器可能不支持此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM