Search code examples
javascriptjqueryajaxpage-refresh

Ajax call when click on Browser refresh Button


Im struggling with Ajax Deep linking and wonder if is possible to make an Ajax call when user click on Browser refresh button.

I tried to use beforeunload It works but still make a request to the server after the ajax call Is there a way to prevent the request to the server ?

    $(window).on('beforeunload',function(e){
        //ajax call here.          
    });

Solution

  • There is no hook into knowing when a user clicks refresh. From a browser perspective they are simply navigating away from the page. You could hook into the onbeforeunload method to do something...

    window.onbeforeunload = function(){ return 'Testing...' }