1

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?

Dhiraj
  • 98
  • 7
  • 1
    Create a cookie on first page load. On every page load, check if the cookie exist, if it does, redirect to new URL with parameter. Or you can use sessionStorage instead of cookies. – Ahs N Apr 13 '16 at 09:51
  • on page refresh whole page get reloaded. In that case if you are getting new url from your server then you can put it in page anywhere using jquery but if you want jquery to put new url on its own then it seems very difficult. – Bhushan Kawadkar Apr 13 '16 at 09:52

2 Answers2

4

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.

Argo
  • 326
  • 3
  • 14
2

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

Community
  • 1
  • 1
CodeLover
  • 271
  • 1
  • 3
  • 14
  • 2
    Rather providing links to the solution write some useful code in your answer. This helps to preventing miss answer contents if links get lost. – Bhushan Kawadkar Apr 13 '16 at 09:56
  • @Bhushan Kawadkar once i did the same thing. Someone reported my answer as steeling the answer. So, after that, i always provide link. – CodeLover Apr 13 '16 at 09:58
  • Then you provide links as well as some relevant code which help others to understand the solution quickly and clearly. There is nothing wrong in referring to other's answers. – Bhushan Kawadkar Apr 13 '16 at 10:01
  • @ Bhushan Kawadka ok. i will. Thanks for your advise. – CodeLover Apr 13 '16 at 10:02