How can I get Bootstrap version via Javascript?
Is there any way to get the Bootstrap version via calling a function? I did some research but couldn’t find any way. The version information is included in the comments at the beginning like this:
`/*!
- Bootstrap v3.3.7 (http://getbootstrap.com)
- Copyright 2011-2016 Twitter, Inc.
- Licensed under the MIT license
*/`
But in case the comments are removed how do I get the bootstrap version? Bootstrap plugins have a version defined in them but I’m looking for the general Bootstrap version, not version of a particular plugin.
The version of each of Bootstrap’s jQuery plugins can be accessed via the VERSION property of the plugin’s constructor. For example, for the tooltip plugin:
$.fn.tooltip.Constructor.VERSION // => "3.3.7"
src //getbootstrap.com/javascript/#js-version-nums
if you mean from the css, then yu ahve to AJAX the file and .match(/v[.\d]+[.\d]/);
You can use this code found here :
var getBootstrapVersion = function () {
var deferred = $.Deferred();
var script = $('script[src*="bootstrap"]');
if (script.length == 0) {
return deferred.reject();
}
var src = script.attr('src');
$.get(src).done(function(response) {
var matches = response.match(/(?!v)([.\d]+[.\d])/);
if (matches && matches.length > 0) {
version = matches[0];
deferred.resolve(version);
}
});
return deferred;
};
getBootstrapVersion().done(function(version) {
console.log(version); // '3.3.4'
});
Checked on the jQuery-less Bootstrap5, to get the version you just neeed
let version = bootstrap.Tooltip.VERSION
This spinoff of the Constructor method adds a fallback plugin and returns an array:
var version = ($().modal||$().tab).Constructor.VERSION.split('.');
You can access the major revision with version[0]
, of course.
you can find bootstrap version here: https://getbootstrap.com/docs/4.3/getting-started/javascript/#version-numbers
$.fn.tooltip.Constructor.VERSION // => "4.3.1"
If you want a solution that works for all the currently supported versions of Bootstrap (3.x, 4.x, 5.x), this should work:
var version = typeof bootstrap === 'undefined' ?
$().tooltip.Constructor.VERSION : bootstrap.Tooltip.VERSION;