First of all, you must save the data received from the ajax request to the local storage browser. Subsequently, in order to show the ajax result when the browser starts "backward", you must bind the operators that you call in the ajax.success() method to the ajax.success() window. To omit code duplication, it is best to use a declared function instead of an anonymous one.
function success(response) { alert("Hi"); $("option", $("#q_subject")).remove(); var option = "<option>Subject1</option>"; option += "<option value=1234>Subject2</option>"; $("#q_subject").append(option); }
Save the data in localstorage and call the success function:
$.ajax({ url: "api.path", type: 'POST', dataType: 'json', data: {id: id}, async: true, cache: false, success: function(response) { localStorage.setItem("response", response); success(response); } });
Call success() when the back button was pressed:
window.onpopstate = function (e) { var res = localStorage.getItem('response'); success(res); }
source share