How to check if Twitter bootstrap is loaded?

前端 未结 9 788
礼貌的吻别
礼貌的吻别 2020-12-07 14:32

How can I check whether there is a bootstrap.js loaded on page (a file bootstrap.js may be compiled into another big JS file)?

相关标签:
9条回答
  • 2020-12-07 14:57

    You can retrieve <script> elements and check their src attribute but as you pointed out, what you are looking for is the code itself and not a file name as the code may be in any file.

    The best way to detect if a JavaScript service/class/etc. is loaded in a page, is to look for something in the DOM that you know is loaded by the given JS.

    E.g. to detect if jQuery is loaded, you can do null !== window.jQuery or to find out the version of the loaded jQuery, jQuery.prototype.jquery

    0 讨论(0)
  • 2020-12-07 14:58

    I would rather check for specific bootstrap plugin since modal or tooltip are very common, so

    if(typeof($.fn.popover) != 'undefined'){
     // your stuff here
    }
    

    or

     if (typeof $.fn.popover == 'function') { 
       // your stuff here
      }
    

    works in both bootstrap versions

    0 讨论(0)
  • 2020-12-07 14:58

    Add the bootstrap link and notice the change in h1 tag.

    0 讨论(0)
提交回复
热议问题