Javascript code
function warnExternalLink(event) {
event.preventDefault(); // Prevent the default link behavior
var externalLink = event.target.href;
if (confirm("You are about to leave our site. Continue?")) {
window.location.href = externalLink;
}
}
// Attach the event listener to all external links
document.addEventListener("DOMContentLoaded", function() {
var externalLinks = document.querySelectorAll('a.external');
externalLinks.forEach(function(link) {
link.addEventListener('click', warnExternalLink);
});
});
template code
{foreach from=$links item=link}
{if $link.external}
<a href="{$link.url}" class="external">{$link.text}</a>
{else}
<a href="{$link.url}">{$link.text}</a>
{/if}
{/foreach}