The classic way of managing javascript scripts is to load all your scripts in your footer/header.
But sometimes you have some pages with special .js
scripts to load (caroussel, extra feature, whatever).
And you do not want your other pages/main page to be affected by this extra load.
You can choose what .js
script to load on a page doing this : Using a partial on all your pages and some variables only where you want the .js
scripts.
Your page frontmatter
In this example we suppose your scripts are in /assets/
folder.
---
url: myurl
js :
- link : "plugins/lightbox2/dist/js/lightbox.min.js"
- link : "plugins/mixitup/dist/mixitup.min.js"
---
Partial (on all your pages) for loading those “page specific” scripts
<!-- Iterate thru your page frontmatter -->
{{- $js := slice }}
{{- $jslocal := false }}
{{- range $.Params.js }}
{{- $js = $js | append (resources.Get .link ) }}
{{- $jslocal = true }}
{{- end }}
<!-- Create the .js really used -->
{{- if $jslocal }}
{{ $nomscript := printf "js/script_%s.min.js" .Params.url }}
{{- $scripts := $js | resources.Concat $nomscript }}
{{- if $isProd }}
{{- $scripts = $scripts | minify | fingerprint "sha384" }}
{{- end }}
<script src="{{ $scripts.RelPermalink }}" {{ if $isProd }}integrity="{{ $scripts.Data.Integrity }}"{{ end }} defer ></script>
{{- end }}
Nota : You can use this logic for your specific .css
too.