change URL of current page on reloading using javascript

By : das

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

By : das

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

This video can help you solving your question :)
By: admin