Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding extra attributes to scripts #2613

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

{% if site.head_scripts %}
{% for script in site.head_scripts %}
<script src="{{ script | relative_url }}"></script>
{% unless script.inline %}
<script src="{{ script.uri | relative_url }}" {{ script.attributes }}></script>
{% else %}
{{ script.inline }}
{% endunless -%}
{% endfor %}
{% endif %}
12 changes: 10 additions & 2 deletions _includes/scripts.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% if site.footer_scripts %}
{% for script in site.footer_scripts %}
<script src="{{ script | relative_url }}"></script>
{% unless script.inline %}
<script src="{{ script.uri | relative_url }}" {{ script.attributes }}></script>
{% else %}
{{ script.inline }}
{% endunless -%}
{% endfor %}
{% else %}
<script src="{{ '/assets/js/main.min.js' | relative_url }}"></script>
Expand All @@ -23,6 +27,10 @@

{% if site.after_footer_scripts %}
{% for script in site.after_footer_scripts %}
<script src="{{ script | relative_url }}"></script>
{% unless script.inline %}
<script src="{{ script.uri | relative_url }}" {{ script.attributes }}></script>
{% else %}
{{ script.inline }}
{% endunless -%}
{% endfor %}
{% endif %}
6 changes: 4 additions & 2 deletions docs/_docs/05-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ For example, to add a CDN version of jQuery to page's head along with a custom s

```yaml
head_scripts:
- https://code.jquery.com/jquery-3.2.1.min.js
- /assets/js/your-custom-head-script.js
- uri: https://code.jquery.com/jquery-3.4.1.min.js
attributes: >
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"
- uri: /assets/js/your-custom-head-script.js
```

Consult the [JavaScript documentation]({{ site.baseurl }}{% link _docs/17-javascript.md %}) for more information on working with theme scripts.
Expand Down
19 changes: 15 additions & 4 deletions docs/_docs/17-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ You can also add scripts to the `<head>` or closing `</body>` elements by adding

```yaml
head_scripts:
- https://code.jquery.com/jquery-3.3.1.min.js
- /assets/js/your-custom-head-script.js
- uri: https://code.jquery.com/jquery-3.4.1.min.js
attributes: >
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"
- uri: /assets/js/your-custom-head-script.js
footer_scripts:
- /assets/js/your-custom-footer-script.js
inline: |
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-12345678-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-12345678-1');
</script>
after_footer_scripts:
- /assets/js/custom-script-loads-after-footer.js
- uri: /assets/js/custom-script-loads-after-footer.js
```

**Note:** If you assign `footer_scripts` the theme's `/assets/js/main.min.js` file will be deactivated. This script includes jQuery and various other plugins that you'll need to find replacements for and include separately.
Expand Down
2 changes: 1 addition & 1 deletion docs/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
{% include scripts.html %}

</body>
</html>
</html>