ojek
ojek

Reputation: 10068

How to prevent page from refreshing

I need to catch refresh event in javascript/jquery. My whole page is being loaded by ajax, I mean all the links and all the forms on my page are being sent by ajax requests. Now, when user refreshes my page, this refresh event is breasking up all I wanted to achieve with my ajax requests, so I really need to catch user refresh calls and stop them, then just forward this refresh event as ajax call.

Is that even possible? And I don't want to catch the F5 keypress - that's insufficient.

Upvotes: 0

Views: 663

Answers (3)

Adib Aroui
Adib Aroui

Reputation: 5067

Unfortunately, the HTML5 History API is inconsistent and buggy, in addition it is implemented differently in all the HTML5 browsers + incompatibility with HTML4 browsers.

Fortunately, History.js is good work that provides cross-compatibility for the HTML5 browsers (ensuring all the HTML5 browsers work as expected) and optionally provides a hash-fallback for HTML4 browsers (including maintained support for data, titles, pushState and replaceState functionality).

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

Demo Fiddle

    <script type="text/javascript">
        window.onbeforeunload = function() {
            return "are you sure to leave this page";
        }
    </script>

Upvotes: 2

epascarello
epascarello

Reputation: 207501

It is not possible to block every form of refresh. Learn about the HTML5 pushState so your users can refresh and use the back/forward buttons with ease.

Upvotes: 4

Related Questions