jsUnitVersionCheck.js
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var versionRequest;
function isOutOfDate(newVersionNumber) {
return JSUNIT_VERSION < newVersionNumber;
}
function sendRequestForLatestVersion(url) {
versionRequest = createXmlHttpRequest();
if (versionRequest) {
versionRequest.onreadystatechange = requestStateChanged;
versionRequest.open("GET", url, true);
versionRequest.send(null);
}
}
function createXmlHttpRequest() {
if (window.XMLHttpRequest)
return new XMLHttpRequest();
else if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
}
function requestStateChanged() {
if (versionRequest && versionRequest.readyState == 4) {
if (versionRequest.status == 200) {
var latestVersion = versionRequest.responseText;
if (isOutOfDate(latestVersion))
versionNotLatest(latestVersion);
else
versionLatest();
} else
versionCheckError();
}
}
function checkForLatestVersion(url) {
setLatestVersionDivHTML("Checking for newer version...");
try {
sendRequestForLatestVersion(url);
} catch (e) {
setLatestVersionDivHTML("An error occurred while checking for a newer version: " + e.message);
}
}
function versionNotLatest(latestVersion) {
setLatestVersionDivHTML('<font color="red">A newer version of JsUnit, version ' + latestVersion + ', is available.</font>');
}
function versionLatest() {
setLatestVersionDivHTML("You are running the latest version of JsUnit.");
}
function setLatestVersionDivHTML(string) {
document.getElementById("versionCheckDiv").innerHTML = string;
}
function versionCheckError() {
setLatestVersionDivHTML("An error occurred while checking for a newer version.");
}