开发者

Is there a way to detect if the user has pressed the refresh button using jquery or javascript?

I want to know if jquer开发者_运维技巧y or javascript can detect if the user has pressed the refresh button of their browser? If so can I see an example?


There is no way to detect if the user pressed refresh before the page refreshes, but you can use cookies to determine if the page refreshed once it loads the second time.

For example, each page could store the location.href in a cookie, and if the last cookie equals the same location.href of the current page, then the user most likely refreshed the page.


window.onunload event will be fired before the page is going to be refreshed.

window.onunload = function(){
    alert("unload event detected!");
}


I'm not sure you can do that, but what you could do instead as a hacky workaround is maybe render the referring page URL from the server into a hidden input or something, and on page load, check to see if the value of the hidden div is the same as the current URL. If it is, it must be a postback or a refresh.


there is 1 way to detect it.

check to see if F5 was pressed. as to detect if the actual refresh was clicked, that i'm not sure if its possible, but you can check against the key code

JS Code .keypress()


Well if you are looking to detect if user press F5 or ctrl+ f5 then you can use following code :)

$("body").keydown(function (e)
{
   if (e.which == 116 || e.which == 17) {
     inFormOrLink = true;
   }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜