Thursday, 23 November 2017

Browser back button event handling in javascript

Browser back button event handling

This is tested code for handling browser back button on chrome browser
 

Confirm Form Resubmission Issue on chrome browser

 <script type="text/javascript">      
        jQuery(document).ready(function ($) {
            if(document.URL == "/cathay-general/news.aspx" || document.URL == "/cathay-general/news")
            {
                $.cookie("visits", document.URL);
            }
            else{$.cookie("visits", "");
            }
            if($.cookie("visits") != "" ){               
                if (window.history && window.history.pushState) {
                    $(window).on('popstate', function () {
                        var hashLocation = $.cookie("visits");//location.hash;
                        var hashSplit = hashLocation.split("#!/");
                        var hashName = hashSplit[1];

                        if (hashName !== '') {
                            var hash = window.location.hash;
                            if (hash === '') {                           
                                window.location.href = "/cathay-general/news.aspx";
                            }
                        }
                    });
                    window.history.pushState('forward', null,$.cookie("visits"));//./#forward "/cathay-general/news.aspx"
                }
            }
        });
</script>