I've a probleme in my code. The aim is to complete a simple form, then you click on a submit button. It do an Ajax resquest to go in the method. On success in the ajax request, i use windows.history.back() to go to the previous page ans here i want to refresh this page, to refresh values which are modificated by the form ! Have you an idea about that ?
$('#form_edit').submit(function (e) { e.preventDefault(); $.ajax({ url: $('#form_edit').attr('action'), type: 'POST', cache: false, data: $(this).serialize(), success: function (data) { if (data === true) { alert("Modification réussie !"); window.history.back(); location.reload(); <= on success i want to refresh previous page } else { alert("Modification échouée !"); } }, error: function () { alert("Modification échouée !"); } }) })
location. reload(true); reloads the page from the server instead of from the cache, window. location. reload(); would do the same thing as location.
Window location. The reload() method reloads the current document. The reload() method does the same as the reload button in your browser.
The location. reload() method reloads the current URL, like the Refresh button.
You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.
window.history.back();
Sometimes it's an issue with javascript compatibility with ajax call or design-related challenges.
I would use this below function for go back with the refresh.
function GoBackWithRefresh(event) { if ('referrer' in document) { window.location = document.referrer; /* OR */ //location.replace(document.referrer); } else { window.history.back(); } }
In your html, use:
<a href="#" onclick="GoBackWithRefresh();return false;">BACK</a>`
For more customization you can use history.js plugins.
This is the correct answer. It will refresh the previous page.
window.location=document.referrer;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With