0

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

Argnist
  • 535
  • 1
  • 5
  • 18

2 Answers2

1

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.

Community
  • 1
  • 1
ccnokes
  • 6,885
  • 5
  • 47
  • 54
1

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.