如何在OpenCart中推迟或异步javascript

Rit*_*mar 10 javascript opencart deferred

我有OpenCart应用程序.Javascripts在settings.php中加载到路径'/catalog/controller//settings.php中,代码如下:

 $this->document->addScript('catalog/view/theme/<theme>/lib/lazy/jquery.lazy.1.6.min.js');
    $this->journal2->minifier->addScript('catalog/view/theme/<theme>/lib/actual/jquery.actual.min.js', 'header');
    $this->journal2->minifier->addScript('catalog/view/theme/<theme>/lib/hover-intent/jquery.hoverIntent.min.js', 'footer');
Run Code Online (Sandbox Code Playgroud)

这里,'theme'表示安装的主题名称.我想在OpenCart中推迟或异步这些javascript加载,我该怎么办呢?

我知道addScript语法有1s参数作为文件,第二个位置,第3个延迟和第4个异步,其中defer和async可以是boolean.我已经尝试过如下语句来查看推迟false和async true:

 $this->journal2->minifier->addScript('catalog/view/theme/<theme>/lib/hover-intent/jquery.hoverIntent.min.js', 'footer', false, true);
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否有效.请建议

LGS*_*Son 3

这是我在 head 元素中使用了很长一段时间的脚本。

这样你就可以很好地控制文件的加载,并且可以在加载所有 DOM 后开始加载任何内容,只需确保加载时 DOM 中的任何位置都不需要这些文件。

<head>
    <title></title>
    <link rel='stylesheet' type='text/css' href='css.css' />
    <script type='text/javascript'>         

        var DomLoaded = {
            done: false, onload: [],
            loaded: function () {
                if (DomLoaded.done) return;
                DomLoaded.done = true;
                if (document.removeEventListener) { document.removeEventListener('DOMContentLoaded', DomLoaded.loaded, false); }
                for (i = 0; i < DomLoaded.onload.length; i++) DomLoaded.onload[i]();
            },
            load: function (fireThis) {
                this.onload.push(fireThis);
                if (document.addEventListener) {
                    document.addEventListener('DOMContentLoaded', DomLoaded.loaded, false);
                } else {
                    /*IE<=8*/
                    if (/MSIE/i.test(navigator.userAgent) && !window.opera) {
                        (function () {
                            try { document.body.doScroll('up'); return DomLoaded.loaded(); } catch (e) { }
                            if (/loaded|complete/.test(document.readyState)) return DomLoaded.loaded();
                            if (!DomLoaded.done) setTimeout(arguments.callee, 10);
                        })();
                    }
                }
                /* fallback */
                window.onload = DomLoaded.loaded;
            }
        };

        DomLoaded.load(function () {
            var d = document;
            var h = d.getElementsByTagName('head')[0];
            var s = d.createElement('script');
            s.setAttribute('type', 'text/javascript');
            s.setAttribute('async', true);
            s.setAttribute('defer', true);
            s.setAttribute('src', '/path/to/scripts.js');
            h.appendChild(s);
        });

    </script>
</head>
Run Code Online (Sandbox Code Playgroud)

这里有一篇很好的文章,描述了更多加快速度的方法,其中之一是将 js 文件组合成 1 或 2-3-4,具体取决于哪个需要哪个。好处是http请求少。

http://exisweb.net/web-site-optimization-making-javascript-load-faster

谷歌上充满了文章

https://www.google.com/search?q=speed%20up%20loading%20javascript%20files&rct=j