*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getElementFromSelector,
EventHandler
.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert.handleDismiss(new Alert()))
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Alert.jQueryInterface
$.fn[NAME].Constructor = Alert
* --------------------------------------------------------------------------
*/
-import { jQuery as $ } from '../util/index'
+import { getjQuery } from '../util/index'
import Data from '../dom/data'
import EventHandler from '../dom/event-handler'
import SelectorEngine from '../dom/selector-engine'
}
})
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .button to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Button.jQueryInterface
$.fn[NAME].Constructor = Button
*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getElementFromSelector,
}
})
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .carousel to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Carousel.jQueryInterface
$.fn[NAME].Constructor = Carousel
*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
})
})
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .collapse to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Collapse.jQueryInterface
$.fn[NAME].Constructor = Collapse
* --------------------------------------------------------------------------
*/
-import { jQuery as $ } from '../util/index'
+import { getjQuery } from '../util/index'
import { createCustomEvent, defaultPreventedPreservedOnDispatch } from './polyfill'
/**
* ------------------------------------------------------------------------
*/
+const $ = getjQuery()
const namespaceRegex = /[^.]*(?=\..*)\.|.*/
const stripNameRegex = /\..*/
const keyEventRegex = /^key/
let defaultPrevented = false
let evt = null
- if (inNamespace && typeof $ !== 'undefined') {
+ if (inNamespace && $) {
jQueryEvent = $.Event(event, args)
$(element).trigger(jQueryEvent)
*/
import {
- jQuery as $,
+ getjQuery,
getElementFromSelector,
isElement,
makeArray,
EventHandler
.on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, e => e.stopPropagation())
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .dropdown to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Dropdown.jQueryInterface
$.fn[NAME].Constructor = Dropdown
*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getElementFromSelector,
data.show(this)
})
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .modal to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Modal.jQueryInterface
$.fn[NAME].Constructor = Modal
* --------------------------------------------------------------------------
*/
-import { jQuery as $ } from '../util/index'
+import { getjQuery } from '../util/index'
import Data from '../dom/data'
import SelectorEngine from '../dom/selector-engine'
import Tooltip from '../tooltip/tooltip'
}
}
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Popover.jQueryInterface
$.fn[NAME].Constructor = Popover
*/
import {
- jQuery as $,
+ getjQuery,
getSelectorFromElement,
getUID,
makeArray,
.forEach(spy => new ScrollSpy(spy, Manipulator.getDataAttributes(spy)))
})
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = ScrollSpy.jQueryInterface
$.fn[NAME].Constructor = ScrollSpy
*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getElementFromSelector,
data.show()
})
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .tab to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Tab.jQueryInterface
$.fn[NAME].Constructor = Tab
*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getTransitionDurationFromElement,
}
}
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .toast to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Toast.jQueryInterface
$.fn[NAME].Constructor = Toast
*/
import {
- jQuery as $,
+ getjQuery,
TRANSITION_END,
emulateTransitionEnd,
findShadowRoot,
}
}
+const $ = getjQuery()
+
/**
* ------------------------------------------------------------------------
* jQuery
* add .tooltip to jQuery only if jQuery is present
*/
/* istanbul ignore if */
-if (typeof $ !== 'undefined') {
+if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Tooltip.jQueryInterface
$.fn[NAME].Constructor = Tooltip
const MAX_UID = 1000000
const MILLISECONDS_MULTIPLIER = 1000
const TRANSITION_END = 'transitionend'
-const { jQuery } = window
// Shoutout AngusCroll (https://goo.gl/pxwQGp)
const toType = obj => ({}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase())
const reflow = element => element.offsetHeight
+const getjQuery = () => {
+ const { jQuery } = window
+
+ if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
+ return jQuery
+ }
+
+ return null
+}
+
export {
- jQuery,
+ getjQuery,
TRANSITION_END,
getUID,
getSelectorFromElement,
expect(Util.reflow(div)).toEqual(0)
})
})
+
+ describe('getjQuery', () => {
+ const fakejQuery = { trigger() {} }
+
+ beforeEach(() => {
+ Object.defineProperty(window, 'jQuery', {
+ value: fakejQuery,
+ writable: true
+ })
+ })
+
+ afterEach(() => {
+ window.jQuery = undefined
+ })
+
+ it('should return jQuery object when present', () => {
+ expect(Util.getjQuery()).toEqual(fakejQuery)
+ })
+
+ it('should not return jQuery object when present if data-no-jquery', () => {
+ document.body.setAttribute('data-no-jquery', '')
+
+ expect(window.jQuery).toEqual(fakejQuery)
+ expect(Util.getjQuery()).toEqual(null)
+
+ document.body.removeAttribute('data-no-jquery')
+ })
+
+ it('should not return jQuery if not present', () => {
+ window.jQuery = undefined
+ expect(Util.getjQuery()).toEqual(null)
+ })
+ })
})
{{< callout warning >}}
## jQuery events
-Bootstrap will detect jQuery only if `jQuery` is present in the `window` object. If jQuery is found, Bootstrap will emit events thanks to jQuery's event system. So if you want to listen to Bootstrap's events, you'll have to use the jQuery methods (`.on`, `.one`).
+Bootstrap will detect jQuery if `jQuery` is present in the `window` object and no `data-no-jquery` attribute on `<body>`. If jQuery is found, Bootstrap will emit events thanks to jQuery's event system. So if you want to listen to Bootstrap's events, you'll have to use the jQuery methods (`.on`, `.one`) instead of `addEventListener`.
{{< highlight js >}}
$('#myTab a').on('shown.bs.tab', function () {