Back to Behavior class

Method polyfill

public static void
polyfill
(mixed $polyfillTypes = null, mixed $conditionalBrowser = null)
Add javascript polyfills.
Parameters
  • string|array $polyfillTypes The polyfill type(s). Examples: event, array('event', 'classlist').
  • string $conditionalBrowser An IE conditional expression. Example: lt IE 9 (lower than IE 9).
Returns
  • void
Since
  • 3.7.0
Class: Behavior
Project: Joomla

Method polyfill - Source code

/**
 * Add javascript polyfills.
 *
 * @param   string|array  $polyfillTypes       The polyfill type(s). Examples: event, array('event', 'classlist').
 * @param   string        $conditionalBrowser  An IE conditional expression. Example: lt IE 9 (lower than IE 9).
 *
 * @return  void
 *
 * @since   3.7.0
 */
public static function polyfill($polyfillTypes = null, $conditionalBrowser = null)
{
    if ($polyfillTypes === null) {
        return;
    }
    foreach ((array) $polyfillTypes as $polyfillType) {
        $sig = md5(serialize(array($polyfillType, $conditionalBrowser)));
        // Only load once
        if (isset(static::$loaded[__METHOD__][$sig])) {
            continue;
        }
        // If include according to browser.
        $scriptOptions = array('version' => 'auto', 'relative' => true);
        $scriptOptions = $conditionalBrowser !== null ? array_replace($scriptOptions, array('conditional' => $conditionalBrowser)) : $scriptOptions;
        /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
        $wa = Factory::getApplication()->getDocument()->getWebAssetManager();
        $wa->registerAndUseScript('polyfill.' . $polyfillType, 'vendor/polyfills/polyfill-' . $polyfillType . '.js', $scriptOptions);
        // Set static array
        static::$loaded[__METHOD__][$sig] = true;
    }
}