I use an accordion or tabs. When clicking on a tab, I want to display a map or something that require a big javascript file to load.
To not load the big javascript when it is not needed, I want to load it only when the tab is clicked.
How can I do that? I've created a good starting point with accordion and a click event.
- I use vanilla js (pure javascript - no jQuery)
- I use it on a webpage (not nodejs)
- I'm fine if it works with modern browsers (IE not needed)
window.addEventListener('DOMContentLoaded', () => {
document.querySelector('[data-trigger]').addEventListener('click', () => {
console.log('Load script from file');
// CDN example - https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.12.0/mapbox-gl.js
});
});
ul {
list-style: none;
margin: 0;
padding: 0;
}
label {
display: flex;
align-items: center;
}
label:before {
content: '';
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'%3E%3Cpath fill='none' d='M0 0h24v24H0z'/%3E%3Cpath d='M13.172 12l-4.95-4.95 1.414-1.414L16 12l-6.364 6.364-1.414-1.414z'/%3E%3C/svg%3E");
background-repeat: no-repeat;
width: 24px;
height: 24px;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ h2 label:before {
transform: rotate(90deg);
}
p {
display: none;
}
input[type=checkbox]:checked ~ h2 ~ p {
display: block;
}
<ul>
<li>
<input type="checkbox" id="faq-1">
<h2 data-trigger>
<label for="faq-1">Click to load map</label>
</h2>
<p>Gummies marzipan croissant chupa chups.</p>
</li>
<li>
<input type="checkbox" id="faq-2">
<h2>
<label for="faq-2">Something else</label>
</h2>
<p>Cookie bear claw carrot cake croissant.</p>
</li>
</ul>