Brad Parks
Brad Parks

Reputation: 72101

Force existing client web pages to reload - using only JSON (no eval)

I'm a consultant working on a web app that's basically a single page app. All it does is constantly retrieve new json data behind the scenes (like once a minute), and then display it on screen.

Our clients load this app, and leave it running 24/7, for weeks on end. If errors happen when retrieving new json data, the app ignores it and keeps running.

We're rolling out an update, and want the existing clients to either become invalidated, or reload themselves without any user interaction. This feature wasn't "built in" by anyone, and we're trying to do this after the fact.

Is there some way to make the existing clients reload without telling our end users to just reload the page?

The following conditions define the app a bit more:

EDIT:

I've had it suggested that we could try the following:

and am open to any suggestions on getting this to reload or kill chrome, so the client is forced to reload the page.

Upvotes: 1

Views: 986

Answers (3)

TheWestIsThe...
TheWestIsThe...

Reputation: 442

If you're using $.ajax to request your data, and not specifically setting your content type, then you may be able to do the following on the server:

  • set the content type header to "text/javascript"
  • respond with javascript, e.g. window.location = "http://www.yoursite.com"

jQuery may eval that, and simply run your javascript.

Upvotes: 2

George Reith
George Reith

Reputation: 13476

No it is not possible. As far as I can tell you do not execute code from the JSON response (which is a very good thing). Thus you have no way of altering your current client's behaviour. According to your own statement:

"Throws away any errors it finds in JSON responses and uses old data"

You will not be able to crash the user's browser by sending invalid JSON data as the errors will be suppressed.

You can build in automatic deployment in to future versions by sending an application version number and testing for changes or by using WebSockets (which the application seems better suited to anyway as you can ensure your clients only poll the server when the JSON has actually changed).

Upvotes: 1

siniradam
siniradam

Reputation: 2929

If I get it correctly, create a version referance page, and make the client check this page very couple seconds, when you update the file, client will reload itself with this script.

var buildNo = "1.2.0.1";//

var cV = setInterval(checkVersion,(5*1000))//Every 5 sec.

function checkVersion(){

    $.ajax({
        url:"checkVersion.php?v="+buildNo,
        dataType:"JSON",
        success:function(d){
            if(d.version != buildNo){//if version is different
                window.location.reload();
                //chrome.runtime.reload(); //for chrome extensions
            }
        }
    })
}

if you cant add extra page, you may just add extra variable to end of your JSON data.

Upvotes: 0

Related Questions