Open external links in a new tab
Every link to another domain opens in a new tab, keeping your current page open.
Code to copy
var host = location.hostname;
document.querySelectorAll('a[href]').forEach(function (a) {
try {
var url = new URL(a.href, location.href);
if (url.hostname && url.hostname !== host && /^https?:$/.test(url.protocol)) {
a.target = '_blank';
a.rel = 'noopener noreferrer';
}
} catch (e) {}
});
How to use this example
- Copy the code with the button above.
- Install JustZix (2 minutes) and open the extension on the target page.
- Add a new rule matching that page.
- Paste the code into the rule's JavaScript panel and save — it runs on every page visit.
Rate this example
No ratings yet — be the first.