Argnist
Argnist

Reputation: 535

How to know in script is Bootstrap 3 added

What is the best and cross-browser way to know in javascript that Bootstrap 3 has already added on the page?

Upvotes: 0

Views: 73

Answers (2)

Boris Tyumnev
Boris Tyumnev

Reputation: 71

This is not the best answer - bootstrap.js adds a lot of extensions to the jQuery - things like $.fn.button. If you are using standard bootstrap package you can check as follows:

if (typeof $.fn.button === 'function' && typeof $.fn.button.Constructor === 'function') {
    // $.fn.button.Constructor.VERSION contains current version 
}

If you don't use bootstrap.js at all or you use custom version of the bootstrap library then this way may not work.

Upvotes: 1

ccnokes
ccnokes

Reputation: 7115

One potential way is to simply check for the presence of the various Bootstrap plugins you're utilizing. For example:

if(typeof $.fn.modal === 'function') {
  //it's on the page
}

All jQuery plugins are added to the jQuery prototype and their existence can be checked on the jQuery.fn property. See How can I check if a jQuery plugin is loaded?

More simply, you could just do:

if($.fn.modal) {
  //it's on the page
}

Looking at Bootstrap's JS source, it doesn't seem to drop any globals that you could check for.

Upvotes: 1

Related Questions