Custom JavaScript UsageYou can write custom JavaScript to target the modal and attach behaviors. The following is an example of how you can use JavaScript to manipulate the Modal component. Please note that Bolt does not ship with any of this custom JavaScript.Demo
Automatically open a modalUse custom JavaScript to automatically open a modal on page load, after a short delay. Click on the "Activate JavaScript" button to see a demo. The page will reload and a modal will open after 3 seconds.
Auto-open Modal
This modal will open 3 seconds after page load.
Custom JavaScript<script>
// Add parameter to the URL - demo helper function, not required in production
var setAutoOpenModalParam = function(set){
var currentUrl = window.location.href.split('?').shift();
var param = '?showModal=true';
window.location.href = set ? currentUrl + param : currentUrl;
}
// Get parameter to the URL - demo helper function, not required in production
var getUrlParameter = function(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
// Reference to the modal
var autoOpenModal = document.querySelector('#js-modal-advanced-auto-open');
// Calls modal 'show' method after a delay
var setAutoOpenModal = function(el) {
if (getUrlParameter('showModal')) {
setTimeout(function(){
el.show();
}, 3000)
}
}
// Callback on modal 'ready'
var onModalReady = function(e) {
setAutoOpenModal(autoOpenModal);
// IMPORTANT: remove this event listener unless you want it called each time the modal component renders
e.target.removeEventListener('modal:ready', onModalReady);
}
// Add 'ready' callback
autoOpenModal.addEventListener('modal:ready', onModalReady);
</script>