Be Part of the Tribe!

    Enjoy free tutorials, share your knowledge!
    Sign up today

    Latest Threads

    Loading products...

External Link warning when leaving site

Edy Lee
Admin
Entrou: 2024-11-24 00:57:42
2025-01-07 22:14:37

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}

 
Jane Marcia
Admin
Entrou: 2024-11-24 00:49:31
2025-01-08 22:30:09

Can you please put that code inside <pre>? It's not easy to copy from a mobile. Thanks