How to detect which Bootstrap version is used from JavaScript?

后端 未结 5 1983
清酒与你
清酒与你 2020-12-29 22:34

I\'ve been looking at the CSS & JavaScript from Bootstrap 3. Nothing is modularized under a common alias. For the JavaScript, it is injected into jQuery\'s prototype...<

5条回答
  •  庸人自扰
    2020-12-29 22:41

    I share my Javascript Module :

    /**
     * Created by LJT.
     */
    myBootstrapUtils = function () {
    
        /**
         * Search bootstrap.min.js or bootstrap.js in DOM
         * @return {*}
         */
        function getBootStrapJsUrl() {
            var bootstrapUrl;
            $('script').each(function () {
                var externalJsSrc = $(this).attr('src');
                if (typeof externalJsSrc !== 'undefined') {
                    if (externalJsSrc.toLowerCase().lastIndexOf('bootstrap.min.js') !== -1 || externalJsSrc.toLowerCase().lastIndexOf('bootstrap.js') !== -1) {
                        bootstrapUrl = externalJsSrc;
                        return false;
                    }
                }
            });
            return bootstrapUrl;
        }
    
        function hasBootStrapJs() {
            return getBootStrapJsUrl() !== undefined;
        }
    
    
        /**
         * This function grab the bootstrap's version in Js File
         * @param data Data file representation
         * @return {*}
         */
        function analyseBootstrapJsFile(data) {
            var bootstrapVersion;
            var matches = data.match(/v[.\d]+[.\d]/);
            if (matches === null) {
                bootstrapVersion = 'unknown';
            } else {
                //Security Array.isArray(matches) === true ?
                bootstrapVersion = matches[0];
            }
            //console.log(bootstrapVersion);
            return bootstrapVersion;
        }
    
    
        function getBootStrapJsVersionSyncMode() {
            var version,
                bootstrapUrl = getBootStrapJsUrl();
            if (bootstrapUrl !== undefined) {
                jQuery.ajax({
                    url: bootstrapUrl,
                    success: function (data) {
                        version = analyseBootstrapJsFile(data);
                    },
                    async: false
                });
            }
            return version;
        }
    
        function getBootStrapJsVersionAsyncMode(defered) {
            var bootstrapUrl = getBootStrapJsUrl();
            if (bootstrapUrl !== undefined) {
                $.get(bootstrapUrl)
                    .then(function (data) {
                        version = analyseBootstrapJsFile(data);
                        defered.resolve(version);
                    })
                    .fail(function () {
                        defered.fail();
                    });
            } else {
                defered.fail();
            }
        }
    
        /**
         * Example for async mode usage
         */
        function exampleAsyncMode() {
            var dfd = $.Deferred();
            dfd.done(function (version) {
                console.log('My bootstrap version is : ' + version);
            });
            dfd.fail(function (version) {
                console.log('Error while request bootstrap version ...');
            });
            getBootStrapJsVersionAsyncMode(dfd);
        }
    
        /**
         * Easy way to get bootstrap plugin version
         * @see http://getbootstrap.com/javascript/#js-version-nums
         * @return {*}
         */
        function getBoostrapModalVersion() {
            return $.fn.modal.Constructor.VERSION;
        }
    
        return {
            hasBootStrapJs: hasBootStrapJs,
            getBootStrapJsVersionSyncMode: getBootStrapJsVersionSyncMode,
            getBootStrapJsVersionAsyncMode: getBootStrapJsVersionAsyncMode
        }
    }();
    
    console.log(myBootstrapUtils.hasBootStrapJs());
    console.log(myBootstrapUtils.getBootStrapJsVersionSyncMode());
    //myBootstrapUtils.exampleAsyncMode();

提交回复
热议问题