如何在 jekyll 中包含来自 /js/ 的 javascript
How to include a javascript from /js/ in jekyll
我添加了来自 here
的 cookie 同意代码
有
<script>
...
if(readCookie('cookie-notice-dismissed')=='true') {
{% include ga.js %}
{% include chatbutton.js %}
}
...
</script>
在html。它位于 _includes
中,但我无法弄清楚如何包含 javascript,如 /js/foo.js
,位于另一个目录中。我相信这是与 jekyll 资产中的捆绑器捆绑在一起的。
到目前为止,我已经通过以下方式在我的布局中添加了 javascript,但还没有为此使用 {% include %}
,我不知道如何让 _includes/cookie_consent.html
知道ẁ这里可以找到。
<script src="/js/foo.js"></script>
<script>
$(function(){
new Foo(".js-foo");
});
</script>
我可以看到两个选项来解决这个问题
- 您在
_includes
中创建了一个文件,其中包含您想要的额外 JS 的 link。
例如:
# /_includes/bar.html
<script src="/js/foo.js"><\/script>
在_cookie_consent.html
里面你可以添加{% include bar.html %}
- 您将 link 直接添加到您的 body。
if(readCookie('cookie-notice-dismissed')=='true') {
const js = '<script src="/js/foo.js"></script>'
document.body.appendChild(js)
}
我添加了来自 here
的 cookie 同意代码有
<script>
...
if(readCookie('cookie-notice-dismissed')=='true') {
{% include ga.js %}
{% include chatbutton.js %}
}
...
</script>
在html。它位于 _includes
中,但我无法弄清楚如何包含 javascript,如 /js/foo.js
,位于另一个目录中。我相信这是与 jekyll 资产中的捆绑器捆绑在一起的。
到目前为止,我已经通过以下方式在我的布局中添加了 javascript,但还没有为此使用 {% include %}
,我不知道如何让 _includes/cookie_consent.html
知道ẁ这里可以找到。
<script src="/js/foo.js"></script>
<script>
$(function(){
new Foo(".js-foo");
});
</script>
我可以看到两个选项来解决这个问题
- 您在
_includes
中创建了一个文件,其中包含您想要的额外 JS 的 link。 例如:
# /_includes/bar.html
<script src="/js/foo.js"><\/script>
在_cookie_consent.html
里面你可以添加{% include bar.html %}
- 您将 link 直接添加到您的 body。
if(readCookie('cookie-notice-dismissed')=='true') {
const js = '<script src="/js/foo.js"></script>'
document.body.appendChild(js)
}