469,325 Members | 1,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,325 developers. It's quick & easy.

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:
http://www.slymail.org/vinocalc.html

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:
http://www.openhosting.co.uk/articles/webdev/5899/

Any help is greatly appreciated.

Cheers
Apr 30 '09 #1
7 2123
Ciary
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
  2.  
  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. }
  17.  
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);
  5.  
  6. echo $data;
  7. ?>
  8.  
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";
  4.  
  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. }
  16.  
  17. function getNewContent(){
  18.     http.open('get','version.txt');
  19.     http.onreadystatechange = updateNewContent;
  20.     http.send(null);
  21.     return false;
  22. }
  23.  
  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>
  33.  
  34. <p><a href="#" onclick="javascript:getNewContent();">Click here to run the function</a></p>
  35. <p id="mySentence"> </p>
  36.  
  37. </body></html>
Apr 30 '09 #3
Ciary
247 Expert 100+
@musther
i wasn't sure if it's possible to call a txt-file with ajax. therefor, i read the file using PHP.

@musther
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
Dormilich
8,651 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?

Thanks
Apr 30 '09 #6
Dormilich
8,651 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
Dormilich
8,651 Expert Mod 8TB
@Ciary
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 suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.