Can We Have Code After Location.Reload(True)

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 the POST 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



Leave a reply



Submit