如何检查本地托管网页的新版本?

时间:2013-12-03 17:02:41

标签: jquery cordova compare

我有一个用HTML / jQuery编写的程序,它驻留在本地机器上,需要在发布更新时通知用户。

我认为程序可以将本地文件(local_version.txt或其他内容)与远程文件(remote_version.txt)进行比较,如果远程文件包含更高的值,则会向用户显示通知。但是,如果用户未连接到互联网,则该功能必须正常失败并且不显示通知。

到目前为止,这是我的代码:

setTimeout(function () {
    $('.updateNotification').show().addClass('heightener');
    callback();
},2000)

function callback() {
  setTimeout(function() {
    $( '.updateNotification' ).fadeOut();
  }, 10000 );
};

我的第一个想法是只比较2个DIV(1个本地加载,另一个远程),但是这个程序显示在用户机器上的webview中(Xcode / VB / PhoneGap,基于平台)所以我不能使用PHP。我希望有一个基于jQuery的解决方案。

1 个答案:

答案 0 :(得分:3)

您可以使用localStorage(甚至是cookie)来存储版本号,并将它们与AJAX请求进行比较。设置如下版本:

localStorage.setItem('version', '0.8');

然后发出一个AJAX请求并比较版本号。

这是我的示例version.json文件:

{"version":0.9}

这是我的要求:

$.getJSON('/version.json', function(response){

  var localVersion = +localStorage.getItem('version'),
      remoteVersion = response.version;

  if( localVersion < remoteVersion ) {
    /* Sweet update action! Isn't the awesome thing about the web that we don't have to "update" anything though? */
  }

});

+之前的localStorage.getItem('version')将其转换为数字而不是字符串,以便我们可以安全地比较它们。 localStorage将所有内容存储为字符串,我认为这很糟糕,但我确信比我更聪明的人有充分的理由。