473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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:
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 2289
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,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?

Thanks
Apr 30 '09 #6
Dormilich
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
Dormilich
8,658 Expert Mod 8TB
@Ciary
all AJAX does is sending a HTTP request to the server.
Apr 30 '09 #8

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

Similar topics

27
by: mrbog | last post by:
Tell me if my assertion is wrong here: The only way to prevent session hijacking is to NEVER store authentication information (such as name/password) in the session. Well, to never authenticate...
33
by: Steven Bethard | last post by:
Is there a good way to determine if an object is a numeric type? Generally, I avoid type-checks in favor of try/except blocks, but I'm not sure what to do in this case: def f(i): ... if x < i:...
3
by: Deano | last post by:
Thought it might be a cool feature to allow my users to click a button, and connect to a webpage that would tell them if a more recent version was available. I can create a button that opens a...
10
by: celoftis | last post by:
Using VS 2005: This morning, following the instructions here: http://ajax.asp.net/docs/overview/InstallingASPNETAJAX.aspx, I installed ASP.NET AJAX on my WinXP Pro development machine. Following...
2
by: trullock | last post by:
Hi, Ive installed the ASP.NET AJAX extensions and ive set up a simple example, however its not doing anything asynchronously, its always posting back... Could this be something to do with the...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
3
by: Mukesh | last post by:
Hi all I have Created an web application using VS 2005, asp.net2.0, Ajax Extensions 1.0, Ent Lib 3.1 , MS sql Server 2005, ajax Control tool kit Version 1.0.10618.0
1
by: Mark B | last post by:
This is my first try at using AJAX. I want the calendars to be enabled if the user checks CheckBox1. It works OK for a normal all page refresh but once I introduced the AJAX code it stopped...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.