Can we have code after location.reload(true)?
The best way to do this would be to write a function that is called if there are certain "GET" variables set and to check them on each run such as mypage.html?reload=true
Or even hash locations: mypage.html#reload
And you can compare like so:
$(document).ready(function () {
if (window.location.hash == '#reload') {
onReload();
}
});
function onReload () {
console.log('test');
}
function reload () {
window.location.hash = 'reload';
window.location.reload();
}
Just call the reload();
function.
Output (in console): test
And then you can just redirect people away from the hash #reload
if you don't want your function to run if the page is refreshed again: window.location.hash = '';
window.location.reload(true) not reloading the page correctly
The problem is that you are mixing ajax with a complete page reload.
The $.ajax
call by default is asynchronous (the first a from ajax...). Therefore it sends a request to increasepostcount.php
and then immediatelly reloads the page - not waiting for increasepostcount.php
to process the request. By the time you manually refresh the page, increasepostcount.php
will have completed the processing of the ajax call, therefore its result are reflected on the page.
If you use ajax, you should not use page reloading. Use javascript to update that part of the page where these records are displayed based on the results returned by the ajax call.
window.location.reload(true) reloads page but page needs refreshing to show changes
Is toggleLive the function that makes the AJAX request? You are calling reload immediately on click before changes are reflected on the backend. If you are using Jquery include your reload code in the complete callback function that indicates completion of your AJAX request.
Reload a page with location.href or window.location.reload(true)
The main difference is follow:
window.location.reload() reloads the current page with
POST
data, while window.location.href='your url' does not include thePOST
data.
Further more, window.location.reload(true)
method reload page from the server. And the browser will skip the cache.
For example, I see you are using success
function from an AJAX
request.
Suppose you have follow method:
[OutputCache(Duration=600)]
public ActionResult Homepage(){
//code here
return View();
}
If you are using window.location.href="location_URL"
,then browser cache data for 600
seconds, which means 10 minutes.
On the other hand, if you use window.location.reload(true)
, then the browser will skip the cache and ,then, reload page from server.
Related Topics
How to Override the Onbeforeunload Dialog and Replace It With My Own
Owl Carousel, Navigation Disabled After Reaching First/Last Item
How to Prevent Browser to Re-Set Inputs on Page Refresh
How to Generate Event Handlers With Loop in JavaScript
Php Ajax Update MySQL Query Onchange from Dropdown (Filtering)
Ask for Geolocation Permission Again If It Was Denied
Filter on Multiple Columns Using One Pipe Angular 2
Fullcalendar - Change View for Mobile Devices
How to Handle Multiple Submit Buttons in a Form Using Angular Js
Jquery Get Select Option Value Empty
Cannot Set Property ... Which Only Has Getter (Javascript Es6)
415 (Unsupported Media Type) With Rest Post Request
Bootstrap Carousel: How to Stop Auto Sliding on Large Screen
Slick.Js: Get Current and Total Slides (Ie. 3/5)
Angular 4 - Cannot Read Property Status of Null While Displaying Validation Error
Ajax - How to Use a Returned Array in a Success Function