Theodor
Theodor

Reputation: 5656

Reload page if modified on server

I am trying to make the page reload if it has changed on the server, by ajax requesting the page's own url every second, and then checking if textStatus is notmodified.

setTimeout(function(){ 
  $.ajax({
    url : window.location.pathname,
    dataType : "text",
    ifModified : true,
    success : function(data, textStatus) {
      if (textStatus !== "notmodified") {
        location.reload();
      }
    }
  });
}, 1000);

However, the textStatus is always success

Upvotes: 2

Views: 1923

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

Just try with a random variable to avoid caching of the response, itself. Also, replace the !== with !=.

setTimeout(function(){ 
  $.ajax({
    url : window.location.pathname + "?" + (new Date()).getMilliseconds(),
    dataType : "text",
    ifModified : true,
    success : function(data, textStatus) {
      if (textStatus != "notmodified") {
        location.reload();
      }
    }
  });
}, 1000);

If this doesn't work, try replacing:

location.reload();

With:

location.href = location.href;

This also depends on the server side script. It also needs to be sent from the Server Side... By setting the no-cache and content-expires.

Upvotes: 1

Related Questions