Thirtynader
Registered
- Thread starter
- #1
Sometimes you may want only certain links to open in a new browser tab, while leaving all other links unchanged.
For example, this is useful if you have:
For example, this is useful if you have:
- A download server on a separate domain
- A URL shortener
- A resume builder or external tools
- Any trusted external service that belongs to your community
JavaScript:
<script>document.addEventListener("DOMContentLoaded", function() {
// Add the domains that should open in a new tab
const externalDomains = [
"example.com",
"downloads.example.com",
"tools.example.com",
"go.example.com"
];
document.querySelectorAll('a[href^="http"]').forEach(link => {
try {
const linkHost = new URL(link.href).hostname.replace('www.', '');
if (externalDomains.includes(linkHost)) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
}
} catch (e) {
// Ignore invalid URLs
}
});
});
</script>
Customisation
Simply edit the externalDomains array to include your own domains:
JavaScript:
const externalDomains = [
"example.com",
"downloads.example.com",
"tools.example.com",
"another-domain.com"
];
Installation
Place the script in any location where custom JavaScript is loaded, such as your XenForo footer template or another global (PAGE_CONTAINER) template that executes JavaScript.Notes
- No CSS is required.
- No additional XenForo add-ons are required.
- The script uses rel="noopener noreferrer" for improved security when opening links in a new tab.
- Each subdomain must be added individually if you want it to be included.