Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 969
Archana Ramachandran Avatar asked Feb 07 '26 03:02

Archana Ramachandran


1 Answers

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.

like image 75
Karan Bhutwala Avatar answered Feb 09 '26 01:02

Karan Bhutwala