7

I'm looking for a way to check/confirm which build of OpenLayers 3 is loaded in a browser. I was expecting that this might be accessible through something like ol.VERSION or ol.getVersion() or similar, but I haven't found anything.

Is there a way to get this information?

7

Searching through the OL code reveals that the version number is included in only two places. Firstly, in the comments on the 3rd line. So if you wanted you could parse the ol.js file itself. But that would be a bit messy.

Secondly, the version number is included in the ol.AssertError message as part of the URL that the error links to. So you can get it by forcing an error to be thrown within a try/catch, and then process the resulting error message to extract the version. This is a bit of a hack, but it works for recent versions at least, and it is likely to work for future versions (but no guarantee!).

The format of the general error message is:

Assertion failed. See https://openlayers.org/en/v3.20.0/doc/errors/#41 for details.

This method relies on this format not changing too much, so you should also do some check afterwards (not included here) to make sure that the result is actually just digits and "." characters (and if not, assign some safe default value like "version could not detected").

Here's what worked for me:

var version;
try {
    var layer = new ol.layer.Vector({ style: new ol.style.Circle({ radius: 30 }) });
}
catch(err) {
    var parts = err.message.split("/v");
    parts = parts[parts.length - 1].split("/");
    version = parts[0];
}
console.log(version);

Code Explanation

  1. Within a try block, throw an OL error (in this case, I passed a non-style data where a style was expected).
  2. In the accompanying catch block, split the error message by the "/v" that occurs immediately before the version number. The version number will then be the first part of the last element of the resulting array.
  3. Then split the last element of that array by "/" (first character after the version number).
  4. Assign the first element of the new array to the version variable.
3
  • Nice hack! It unfortunately doesn't work with v3.11.2 that I'm running, but your approach of just searching the source file shows it's impossible: the information isn't stored anywhere other than the comment, which is inaccessible at runtime. I'm using webpack to build this project, so maybe it's possible to have it extract and include the version number somehow. – dbaston Dec 16 '16 at 14:16
  • The comment in the source code is not inaccessible at run time - this is Javascript after all :-) . See my other answer. It was actually written first, but then deleted when I came up with this other method. I've undeleted my other answer now. – Son of a Beach Dec 16 '16 at 21:19
  • Great answer. Still working for OL 5.3.0 – Alex Pandrea Dec 12 '18 at 4:02
5

As of OpenLayers 4.1.0, the version number can be accessed through the ol.VERSION variable.

1
  • 1
    The Question asked for OL3, the answer is for OL4 – neogeomat Sep 30 '18 at 8:55
1

So I don't don't know if this applies to v3 but as of v4 the easiest way I've found of doing this is to access it through the index file.

import olIndex from 'ol/index'

const olVersion = olIndex.VERSION

console.log(olVersion)
1

For OpenLayers 5.3 with Angular 6:

import * as OLUtil from 'ol/util';

Then:

console.log('OpenLayers Version: ',OLUtil.VERSION);

enter image description here

1
  • It's actually a typescript syntax for use with NPM. You can tell because the extension (.js) is missing. – Rivenfall Mar 4 '20 at 10:13
0

So long as they don't change the format of their distribution, you could parse the 'ol.js' file itself. At least in the current/recent versions, the version is in the 3rd line of the file (first 3 lines are comments).

The first 3 lines of ol.js look something like:

// OpenLayers 3. See http://openlayers.org/
// License: https://raw.githubusercontent.com/openlayers/ol3/master/LICENSE.md
// Version: v3.18.2

You could use Javascript's XMLHttpRequest or FileReader to read the contents of your ol.js URL or local file (depending on where your ol.js is). Then grab the 3rd line, split it on v (lowercase 'v', not uppercase!) and take the last item of the split as the version identifier.

However, since there is the possibility of this format changing, you'd want to build in some sort of fail-safe functionality, so that if your result was not all digits and '.', then produce some warning output that the version could not be determined.

XMLHttpRequest: http://www.w3schools.com/xml/xml_http.asp

FileReader: http://www.javascripture.com/FileReader

split: http://www.w3schools.com/jsref/jsref_split.asp

0

The version is in a variable which you can see easily in github openlayers/openlayers/src/ol/util.js.

import {VERSION} from 'ol/util.js' should work if you are using NPM and a recent enough version.

This is private to NPM which is annoying but I guess you don't need it in production with an ol.min.js file which has no associated source map.

To see this variable in ol.js[.map] (which should be called ol.min.js[.map]) you could search the code with some regex \d\.\d\.\d.

-2

You can try to see if a specific ol3 function exists:

if (typeof ol.inherits === "function") {
  console.log("ol3 is loaded");
} else {
  console.log("ol3 is not loaded");
}
2
  • That tells me if it's loaded, but not which version is loaded. – dbaston Jul 1 '16 at 13:25
  • True, sorry. Please downvote my "answer". What's left I think is parsing it from the source, or asking the devs to add a version function. – peter Jul 4 '16 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.