• Developing a plugin that uses Twitter’s Bootstrap Modals, but would like to prevent loading bootstrap if it’s already loaded.

    Apparently the following .js code could do the trick:

    <script> if(typeof($.fn.modal) === 'undefined') {document.write('<script src="//www.mysite.com/js/v/bootstrap-2.1.1.js"><\/script>')}</script>

    But is there any way of doing it with php, or otherwise preventing the plugin from encoding the script if it’s already present?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    All javascript in WP should be introduced onto a page by way of wp_enqueue_script(). When done properly, the script will only be loaded when it’s needed, and only once. WP works out dependencies to ensure this.

    Thread Starter mikeill

    (@mikeill)

    I know. But i think that in the case of the customizr theme, the bootstrap functionality may be included in other .js files, so that if WP were checking for a specific file name the enqueue check wouldn’t work.

    Moderator bcworkz

    (@bcworkz)

    PHP cannot prevent the loading of script it has no knowledge about. It will not send out duplicate script links if they are properly enqueued, but if a couple of other javascripts both try to load the bootstrap, there is nothing PHP can do about it, this is happening on the browser, not the server. It’s the responsibility of the code doing the loading of a common file to ensure it has not already been loaded by using logic similar to your example above.

    I’ve no idea about Twitter modals, but bootstraps are typically relatively small so the reloading shouldn’t make a huge impact. In fact, bootstraps are often placed so that larger libraries can be loaded in an orderly manner regardless of any mess the page creator may make of loading scripts.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Check if Twitter Bootstrap is already loaded’ is closed to new replies.