jarus
jarus

Reputation: 1873

Back Button and Refresh with AJAX

I need a solution for the page refresh and the back button when using AJAX.

I'm using simple javascript for the AJAX implementation.

If anybody could send a code snippet I would be very grateful.

Upvotes: 4

Views: 4820

Answers (5)

gekong
gekong

Reputation: 125

Here's a solution that I've used in some of my pages. Add this to pages that changes are made at.

window.onbeforeunload = function() {
     window.name = "reloader"; 
}

this triggers when you leave those pages. You can also trigger it if there were changes made. So that it won't unnecessarily reload the page that needs reloading. Then on pages that you want to get reloaded on after a the browser "back" use.

if (window.name == "reloader") {
    window.name = "no";
    location.reload();
}

this will trigger a reload on the page you need reloading to.

Upvotes: 3

Nikita Koksharov
Nikita Koksharov

Reputation: 10763

Try PathJS it does not require jQuery or any other additional lib.

Upvotes: 0

Sarel Botha
Sarel Botha

Reputation: 12700

The onbeforeunload event can be useful to guard against refreshing but it fires if you navigate away or refresh. If you require that users login to the app you can always show a generic message advising against navigating away and refreshing. If users click your app log out button set a var to disable the warning. Could probably also make a 'Close' button that does the same thing.

Upvotes: 0

Tracker1
Tracker1

Reputation: 19334

essentially, you need to use & monitor the hash portion of the url...

http://.../path?parms#hashpart

Whan you change the hash, iirc window.location.hash , it won't reload the page, but your ajax can monitor, and respond to it.

Upvotes: 1

Chad Birch
Chad Birch

Reputation: 74518

If you're using jQuery, there's the history plugin.

Upvotes: 5

Related Questions