Dhiraj
Dhiraj

Reputation: 98

Update URL on f5 or refresh button

i am trying to update url when the user hits f5 key or refresh the page using browser refresh button so that i can get the update value for key, for eg: if my current url is :

www.examplexyz.com?key=abcd

so if user hits f5 key , now i want to update the url as :

www.examplexyz.com?key=newabcd

is there any way of doing it using jquery? i tried to search it on google but what i found is disable f5. so how can i achieve this?

Upvotes: 1

Views: 2059

Answers (2)

Argo
Argo

Reputation: 335

You can listen to the f5 key down.

$("body").keydown(function(e){
   alert("keydown: "+e.which);
   e.preventDefault();
});

PreventDefault() disable the default browser refreshing action. The value of F5 keydown is 116. So you can go on it in this way.

$("body").keydown(function(e){
    if(e.which==116){
        alert("keydown: "+e.which);
        e.preventDefault();
    }
});

Then you can do what you want inside the "if" scope, for example load your custom url.

window.location.href = "http://www.google.com";

I'm assuming that you know the url you want to load or at least how to build it.

Upvotes: 4

CodeLover
CodeLover

Reputation: 281

Yes , you can do this. First of all you have to listen the f5 press event. You can get help from here.. After that, you have to update your query string, for this you can get help from here. I think, it will help you

Upvotes: 2

Related Questions