Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 249
jarus Avatar asked Jan 23 '23 19:01

jarus


2 Answers

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

like image 146
Chad Birch Avatar answered Feb 01 '23 12:02

Chad Birch


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.

like image 20
gekong Avatar answered Feb 01 '23 13:02

gekong