htmlHelper::jsLoad PHP Method

jsLoad() public method

Version is optional and can be exact (1.8.2) or just version-major (1 or 1.8) Usage: echo $html->jsLoad('jquery'); echo $html->jsLoad(array('yui', 'mootools')); echo $html->jsLoad(array('yui' => 2.7, 'jquery', 'dojo' => '1.3.1', 'scriptaculous')); You can also use the Google API format JSON-decoded in which case version is required & name must be lowercase $jsLibs = array(array('name' => 'mootools', 'version' => 1.2, 'base_domain' => 'ditu.google.cn'), array(...)); echo $html->jsLoad($jsLibs);
public jsLoad ( mixed $library, mixed $version = null, array $options = [] ) : str
$library mixed Can be a string, array(str1, str2...) or , array(name1 => version1, name2 => version2...) or JSON-decoded Google API syntax array(array('name' => 'yui', 'version' => 2), array(...))
$version mixed Optional, int or str, this is only used if $library is a string
$options array Optional, passed to Google "optionalSettings" argument, only used if $library == str
return str
    public function jsLoad($library, $version = null, array $options = array())
    {
        $versionDefaults = array('swfobject' => 2, 'yui' => 2, 'ext-core' => 3, 'mootools' => 1.2);
        if (!is_array($library)) {
            //jsLoad('yui')
            $library = strtolower($library);
            if (!$version) {
                $version = !isset($versionDefaults[$library]) ? 1 : $versionDefaults[$library];
            }
            $library = array('name' => $library, 'version' => $version);
            $library = array(!$options ? $library : array_merge($library, $options));
        } else {
            foreach ($library as $key => $val) {
                if (!is_array($val)) {
                    if (is_int($key)) {
                        //jsLoad(array('yui', 'prototype'))
                        $val = strtolower($val);
                        $version = !isset($versionDefaults[$val]) ? 1 : $versionDefaults[$val];
                        $library[$key] = array('name' => $val, 'version' => $version);
                    } else {
                        if (!is_array($val)) {
                            // //jsLoad(array('yui' => '2.8.0r4', 'prototype' => 1.6))
                            $library[$key] = array('name' => strtolower($key), 'version' => $val);
                        }
                    }
                }
            }
        }
        $url = $this->_protocol . 'www.google.com/jsapi';
        if (!isset($this->_includedFiles['js'][$url])) {
            //autoload library
            $this->_includedFiles['js'][$url] = true;
            $url .= '?autoload=' . urlencode(json_encode(array('modules' => array_values($library))));
            $return = $this->js($url);
        } else {
            //load inline
            foreach ($library as $lib) {
                $js = 'google.load("' . $lib['name'] . '", "' . $lib['version'] . '"';
                if (count($lib) > 2) {
                    unset($lib['name'], $lib['version']);
                    $js .= ', ' . json_encode($lib);
                }
                $jsLoads[] = $js . ');';
            }
            $return = $this->jsInline(implode(PHP_EOL, $jsLoads));
        }
        return $return;
    }