change URL of current page on reloading using javascript

My requirement is to change the URL of the current page on clicking refresh icon or ctrl+R. That is on refresh, reload another page(different URL). And it should support all browsers. I have tried the below snippet. But its not working.

window.onbeforeunload = function(e) {
  window.location.href = '';

where ‘’ should be the URL of my page on refreshing the current page. But it’s nothappening with the window.onbeforeunload().

  • Angular2 Nested Inputs in components in a form - controls array is empty
  • How to iterate through an Object attributes in Angular 2
  • Add event binding to dynamically created components
  • Angular2 not working with basic javascript library
  • Angular 2 ng-bootstrap Modal: How to pass data to entry component
  • Disabling Server-Side Rendering using Angular 2 Webpack starter
  • Angular 2: How do you render HTML from a JSON response without displaying the tags to the user?
  • Angular 2 in Phonegap doesn't work (plain javascript)
  • Can I catch certain errors before “subscribe()” in an RXJS observable in Angular2?
  • Shared RxJS subject for bidirectional data binding in Angular 2
  • Hash Location Strategy in Angular 2
  • DialogAPI with Angular 2 : Diasplay my angular 2 component inside dialog using displayDialogAsync
  • 2 Solutions collect form web for “change URL of current page on reloading using javascript”

    OnBeforeUnload needs a return value to work, but actually, it will only display a message asking the user if he’s sure that he wants to leave the page (just like Facebook does when you try to leave a page with some pending changes).

    As far as I know, web browsers don’t want to give you control over that, mainly due to websites redirecting users when they try to leave and stuff like that, so there’s nothing like a “standard way” to do it and even if you find a workaround, I’m pretty sure that it will not work on all browsers.
    But why do your customer wants that behaviour on his website? At least in my opinion, the only thing that he will get with that is a horrible user experience for his end users.

    As @Babydead states, this seems to be a duplicated:

    Modify the URL without reloading the page

    But, if you’re looking for a quick solution, just try:


    Don’t forget to declare the protocol “http”.