Skip to content Skip to sidebar Skip to footer

Change The Url On Page Refresh

I m currently on a page www.example123.com/search?error=1 . On page refresh, the page should be loaded with the following url. www.example123.com/search or www.example123.com

Solution 1:

I did it using unload event. This event will be called when a page is refreshed.

$(window).unload(function() {
     var currentURL = window.location.href;
     var index = currentURL.indexOf("?error=");
     if(index > -1) {
         window.location.href = currentURL.substring(0, index);
     }
});

Then the page will be refreshed with the new URL.

Let me know if this will not work in any case.

Note: It doesnt work in chrome. How to make it work in chrome?

Solution 2:

You can detect if the page has refreshed with a cookie, and then do something like the following:

if(refresh==1){
  if(window.location.href=='http://www.example123.com/search?error=1'){
    window.location.replace = "http://www.example123.com/search";
  }
}

Post a Comment for "Change The Url On Page Refresh"