0
$.getJSON("https://api.github.com/repos/theinfection/screencalculator.kdapp/contents/resources/version", function(data) {
    var currentVersion = decode64(data.content);

    $.get('./resources/version', function(dataVersion){
        var myVersion = dataVersion;

        if (currentVersion > myVersion) {
            $('.update').show();
        }
    });
});

Explanation of the code

First, I get the contents of a file on my GitHub repository called "version" (The file has "1.4.3" written in it). Then, since GitHub encodes the contents of files in base64, I decode it with another JS file. This sets the "currentVersion" variable to 1.4.3.

Next, I call the "version" file I have on my server (which has 1.4.2 written in it). That sets the "myVersion" variable to 1.4.2.

From there, I compare the two. If currentVersion is greater than myVerison, it shows the update DIV (which by default is set to "display:none;" in the CSS).

Problem

When the two variables are equal, the DIV still shows.

Question

What should I do so that this doesn't happen? How exactly should I be using the else and else if statements to accomplish what I want?

Thanks in advance!

Sinan
  • 11,443
  • 7
  • 37
  • 48
Kasim Ahmic
  • 312
  • 3
  • 15

3 Answers3

2

It looks like your problem could be that 1.4.2 and 1.4.3 are not numbers and would be represented as string. You would need to add some logic like splitting the string by the period and doing checks on the sub versions.

Here is a post about comparing version numbers with JS that might solve your issue.

How to compare software version number using js? (only number)

EDIT

You can use the method from the post I linked to do the comparison for your versions

function version_compare(curVersion, myVersion) {
    var v1parts = curVersion.split('.');
    var v2parts = myVersion.split('.');

    for (var i = 0; i < v1parts.length; ++i) {
        if (v2parts.length == i) {
            return false;
        }

        if (v1parts[i] == v2parts[i]) {
            continue;
        }
        else if (v1parts[i] > v2parts[i]) {
            return false;
        }
        else {
            return true;
        }
    }

    if (v1parts.length != v2parts.length) {
        return true;
    }

    return false;
}

$.getJSON("https://api.github.com/repos/theinfection/screencalculator.kdapp/contents/resources/version", function(data) {
    var currentVersion = decode64(data.content);

    $.get('./resources/version', function(dataVersion){
        var myVersion = dataVersion;

        if (!version_compare(currentVersion,myVersion)) {
            $('.update').show();
        }
    });
});

I modified the method a little to return a bool instead of a string of which version is larger.

Community
  • 1
  • 1
DSlagle
  • 1,563
  • 12
  • 19
  • It still doesn't work. Both of the version files are set to 1.4.3 yet the DIV still shows up. By default the DIV should be hidden and should show up if and only if myVersion (the version that I have) is smaller than currentVersion (the version on GitHub). If myVersion is greater than or equal to currentVersion, nothing should happen and the DIV should remain hidden. – Kasim Ahmic May 30 '13 at 23:26
  • Made a change to return false if the versions match – DSlagle May 30 '13 at 23:27
  • That didn't help either. I made a jsFiddle in hopes that it'll help you: http://jsfiddle.net/URP89/ – Kasim Ahmic May 30 '13 at 23:45
  • I updated your jsFiddle. I had to add a decode64 function and because of cross domain issue it wouldn't load your resource file from your server so I statically set the version number and it is working. If you are still having issues make sure your var currentVersion has the correct value and that the get request to your version file on your server returns the correct version. http://jsfiddle.net/URP89/2/ – DSlagle May 31 '13 at 00:11
  • Well after taking a look at the code that @zshift provided and put it in my site, I found out that for whatever reason, after the version file is decoded, several unknown characters are added to the currentVersion variable. So now, instead of using GitHub to hold the currentVersion file, I'll just host it on my site and updated it manually. I think this may be an issue with my host and I'll definitely take a look at this again at a later date, but for now, I have what I need. Thank you so much for your help! – Kasim Ahmic May 31 '13 at 20:24
  • You might want to take a look into your decode64 method you are using. The one that I added to the jsfiddle I pulled from the first site I found after searching for js base64. It seems to be working with that method and it is pulling the version from the github api and decoding it correctly. – DSlagle Jun 01 '13 at 18:05
0

I've read through the post by DSlagle, and made some updates here after going through some of the comments that had changes on jsfiddle.

http://jsfiddle.net/zshift/BCGSb/

    var currentVersion = decode64(data.content);
    alert('Current version: ' + currentVersion);
    //$.get('http://kasimahmic.koding.com/works/PPI/resources/version', function(dataVersion){
    var myVersion = '1.4.2';
    alert('Local version: ' + myVersion);

    if (currentVersion > myVersion) {
        alert('update available');
        $('.update').show();
    }
    else
        alert('no update available');

The comparison should work fine with data from the server, but that depends on what your local resource file is returning. Since we can't debug directly against your data due to access control rights, I mocked out the call, and using 1.4.3 for both of the values resulted in the DIV not being shown. Changing 'myVersion' to 1.4.2 caused it to be shown. I also added the added some alerts to make it a little easier to see the values and flow for debugging.

Another note on debugging with jsfiddle. Using Chrome, you can easily debug running code in jsfiddle, just open the developer tools, go to the sources tab, open fiddle.jshell.net, and if your fiddle is at http://jsfiddle.net/{id}, then the source should be in a folder called {id}/show. This is the frame in the bottom right of jsfiddle. You can also inspect the page and click on the link in the iframe, this will navigate you to the same source window. from here, you can attach breakpoints and debug with live data. The reason I didn't bring this up before is that your code is running as soon as the page loads, so it's not possible to watch it live without calling a separate function after the page loads. But thought it might be useful for future reference ;)

debugging jsfiddle in chrome

zshift
  • 341
  • 5
  • 10
0

This can be used as a simple version comparator :

function Version(parse) {
this.version = parse.split('.');

this.compare = function(comp) {
    for(i = 0; i < this.version.length; i++) {
        if(this.version[i] < comp.version[i]) {
            return "older";
        }else if(this.version[i] > comp.version[i]) {
            return "newer";
        }
    }
    return "same";
};
};

var ver = new Version("1.2.5");
var ver2 = new Version("1.2.4");

console.log(ver.compare(ver2)); // newer
console.log(ver.compare(ver)); // same
console.log(ver2.compare(ver)); // older

You can edit so it will return e.g. 0,1 or true,false, depends on you.

Gelidus

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
petomalina
  • 2,020
  • 2
  • 19
  • 25