(function() {
// Function to update links
function updateExternalLinks(context) {
const links = (context || document).querySelectorAll('a[href]');
links.forEach(link => {
try {
const url = new URL(link.href, window.location.origin);
if (url.origin !== window.location.origin && !link.hasAttribute('target')) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
}
} catch (e) {
// Ignore invalid URLs
}
});
}
// Initial scan on page load
document.addEventListener('DOMContentLoaded', () => updateExternalLinks());
// Also watch for dynamically added links (e.g., Elementor Loop Grid)
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1) {
updateExternalLinks(node);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
})();