如何检查 Twitter 引导程序是否已加载?

2022-01-19 00:00:00 frontend javascript twitter-bootstrap

如何检查页面上是否加载了 bootstrap.js 文件(bootstrap.js 文件本身可能会被编译/缩小为另一个更大的 JS文件)?

How can I check whether there is a bootstrap.js file loaded on a page (the bootstrap.js file itself may be compiled/minified into another, bigger JS file)?

推荐答案

您需要做的只是检查 Bootstrap 特定的方法是否可用.我将在此示例中使用模态(适用于 Bootstrap 2-4):

All you need to do is simply check if a Bootstrap-specific method is available. I'll use modal in this example (works for Bootstrap 2-4):

// Will be true if bootstrap is loaded, false otherwise
var bootstrap_enabled = (typeof $().modal == 'function');

它显然不是 100% 可靠的,因为模态函数可以由不同的插件提供,但它仍然可以完成这项工作......

It is not 100% reliable obviously since a modal function can be provided by a different plugin, but nonetheless it will do the job...

您还可以更具体地检查 Bootstrap 3-4(从 3.1+ 开始工作):

You can also check for Bootstrap 3-4 more specifically (works as of 3.1+):

// Will be true if Bootstrap 3-4 is loaded, false if Bootstrap 2 or no Bootstrap
var bootstrap_enabled = (typeof $().emulateTransitionEnd == 'function');

请注意,所有这些检查都要求 jQuery 已加载.

Note that all of these checks require that jQuery is already loaded.

相关文章