简体   繁体   中英

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

You can try a counter kind of thing. if counter is 2 than redirect to your required page.

or you can try document.referrer but some browser may not support this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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