Liberat0r
Liberat0r

Reputation: 1871

Change location.hash while reloading the page

I am keeping some info about filters that I use on the page in the location.hash so if a user sends a link to another user, he has the first one's filters applied. However I would like that if first user refreshes the page to clean location.hash and reset filters. First thing that I thought about was to clean the hash on unload event but it doesn't seem to work as I wanted. The code:

$(window).on('unload', function(){ window.location.hash = ''; });

Cleans location.hash after pressing F5, but then it holds and I must press F5 again to refresh my page. So I wrote:

$(window).on('unload', function(){ window.location.hash = ''; window.location.reload(true); });

But it doesn't change anything. I was also experimenting with onbeforeunload event, but in it's scope I cannot change the hash. Could someone help me with this?

Upvotes: 0

Views: 162

Answers (1)

PinkSFer
PinkSFer

Reputation: 31

I had a similar issue. Got it to work using a timeout.

$(window).on('unload', function(){ 
    window.location.hash = ''; 
    setTimeout(function () { window.location.reload(true); }, 0); 
});

Upvotes: 1

Related Questions