471,887 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

JS/Ajax to check a file for a version number, and compare to known value.

I have a set of calculators written in JS on a page:

The page is designed to be downloaded and used offline, or simply saved as a file for speed of access and reliability (not always on internet connections).

Anyway, I would like to store a file on the server, which contains the current version number (such as 2.0). That's all the file will contain. Then when the page is loaded, it will access that version file, and compare the version in that file to the hardcoded current version number.

The upshot is, if somebody is using the file from their local machine, the code tries to open the version file on the server (if it fails, we assume we're offline), it the compares the current version to the known version being used, and if they're not the same, displays a descrete message on the page somewhere.

Of course, if the user is offline, I don't want the page to delay loading while it tries to load the version file and times out, so the Ajax will have to do that in the background.

As you might have guessed, I'm new to ajax, but do have some JS experience.

I've been doing a lot of googling, and think that a modification of the code here may be way to go:

Any help is greatly appreciated.

Apr 30 '09 #1
7 2179
247 Expert 100+
i think, reading files on the server is only possible using PHP. which gives you another problem:
when you're offline, php is unavailable. what you can do is read in an ajax request. then, if the request fails, you presume your user is offline. i think the solution isnt that hard. just put a try catch around the ajax-request. for the messagebox: try using this:
Expand|Select|Wrap|Line Numbers
  1. var Version = "2.0";   //assigned globally
  3. function compareVersion(){
  4.    try{
  5.       XMLHttpRequestObject = GetXmlHttpObject();
  6.       var version = "version.php;
  7.       XMLHttpRequestObject.open("GET",version,true);
  8.       XMLHttpRequestObject.onreadystatechange = function(){
  9.       if (XMLHttpRequestObject.readyState == 4){
  10.          var txtversion = XMLHttpRequestObject.responseText;
  11.          if(Version != txtversion){
  12.             alert("this is an old version");
  13.          }
  14.       }
  15.    }catch(ex){}
  16. }
in php you do this

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $id = fopen("version.txt", "r");
  3. $data = fread($id, filesize("version.txt"));
  4. fclose($id);
  6. echo $data;
  7. ?>
hope that helps
Apr 30 '09 #2
I'm not sure I quite follow. Why is the PHP required - you're calling a PHP file, which is giving the result, why not just call the text file with the version number directly?

I've been having a go, and come up with the following. So far, I know the versions are correctly in the variables when it comes to the comparison, but for some reason the comparison always seems to return not-equal, even when NewVer and Version both contain 2.0.

Expand|Select|Wrap|Line Numbers
  1. <html><body>
  2. <script type="text/javascript">
  3. var Version = "2.0";
  5. var http = createRequestObject();
  6. function createRequestObject() {
  7.     var objAjax;
  8.     var browser = navigator.appName;
  9.     if(browser == "Microsoft Internet Explorer"){
  10.         objAjax = new ActiveXObject("Microsoft.XMLHTTP");
  11.     }else{
  12.         objAjax = new XMLHttpRequest();
  13.     }
  14.     return objAjax;
  15. }
  17. function getNewContent(){
  18.     http.open('get','version.txt');
  19.     http.onreadystatechange = updateNewContent;
  20.     http.send(null);
  21.     return false;
  22. }
  24. function updateNewContent(){
  25.     if(http.readyState == 4){
  26.         var NewVer = http.responseText;
  27.               if(Version != NewVer){
  28.                  alert(NewVer);
  29.               }
  30.     }
  31. }
  32. </script>
  34. <p><a href="#" onclick="javascript:getNewContent();">Click here to run the function</a></p>
  35. <p id="mySentence"> </p>
  37. </body></html>
Apr 30 '09 #3
247 Expert 100+
i wasn't sure if it's possible to call a txt-file with ajax. therefor, i read the file using PHP.

check spaces and newlines in your txt. if there arent any try this

Expand|Select|Wrap|Line Numbers
  1. if(parseInt(Version) != parseInt(NewVer))
just to make sure it isn't a type matter.

i hope it helped
Apr 30 '09 #4
8,658 Expert Mod 8TB
I could imagine something without using AJAX.

place your version info in a .js file on the server and make your server this file must-revalidate (setting headers).

if you call this file in the script you get the version number from the server (or if no connection is available the local copy) already in JS format ready to use.
Apr 30 '09 #5
Dormilich, that sounds like a really good idea - could you please give me an example of how to code it?

Apr 30 '09 #6
8,658 Expert Mod 8TB
the JS part is quite easy
Expand|Select|Wrap|Line Numbers
  1. window.version = "1.0";
the HTML part too
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="http://example.org/version.js"></script>
the server part depends on which server you have.
Apr 30 '09 #7
8,658 Expert Mod 8TB
all AJAX does is sending a HTTP request to the server.
Apr 30 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

27 posts views Thread by mrbog | last post: by
33 posts views Thread by Steven Bethard | last post: by
2 posts views Thread by trullock | last post: by
1 post views Thread by geevaa | last post: by
1 post views Thread by Mark B | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.