473,834 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 2309
247 Recognized Expert New Member
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
3 New Member
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 Recognized Expert New Member
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 Recognized Expert Moderator Expert
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
3 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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

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 a user from information you got from the session. Each secure app on a site must challenge the user for name and password, each and every time the user accesses it (not just once and then store it in the session). If a secure app is multi-page,...
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: ... The problem is, no error will be thrown if 'i' is, say, a string:
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 webpage but it would be nice if it could compare what they have with what I say is available, so I can say something like "You have version 1.2. Version 1.3 is now available". Any ideas?
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 that, I installed the "November 2006 CTP" and "Control Toolkit" - then I re-booted my machine. Next I tried to create a simple, sample page... I have the AJAX controls in my toolbox, but when I drag on onto my page it gets underlined - denoting...
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 fact ive added it to an existing project not an "asp.net ajax enabled project"? If so, whats the difference and how do i make my existing project the
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 technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
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
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 working. Any ideas? <%@ Page Language="VB" AutoEventWireup="false" CodeFile="default-ajax.aspx.vb" Inherits="pages_verify_groups_Default" Debug="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.