How can I make all external links open in a new tab by default on my Webflow site, while keeping internal links opening in the current tab? Is there a solution that doesn't involve manually ticking a box for each link? Thank you!

TL;DR
  • Navigate to your Webflow Project Settings, insert the JavaScript snippet in the Custom Code tab to automatically open external links in new tabs by checking each link for the site's hostname.
  • Save changes and publish the project, then verify functionality by testing pages to ensure external links open in new tabs and internal links don't.

Automating the opening of external links in a new tab on your Webflow site can enhance user experience without requiring manual adjustments per link.

1. Use Custom Code in Your Project

  • Go to Project Settings: Access your Webflow project and navigate to the Project Settings.
  • Choose the Custom Code Tab: Select the Custom Code tab where you can insert JavaScript to modify link behaviors.
  • Insert JavaScript Logic: Use custom JavaScript to automatically add target="_blank" to all external links while keeping internal links opening in the current tab. Insert the following line of script in the Before </body> tag section:
  • document.querySelectorAll('a[href^="http"]').forEach((link) => { if (!link.href.includes(window.location.hostname)) { link.setAttribute('target', '_blank'); } });
  • This script checks each link to see if it starts with "http" and does not contain the site's own hostname, then assigns target="_blank" to qualifying links.

2. Save and Publish the Project

  • Save the Changes: After adding the script, make sure to save the changes in the Project Settings.
  • Publish Your Site: Select Publish for the changes to take effect across your site.

3. Verify That the Code Works

  • Test the Functionality: Visit various pages on your site to ensure external links now open in new tabs and internal links remain in the current tab.

Summary

In summary, by adding a small JavaScript snippet in the Custom Code section of your Webflow project, you can automate the process, ensuring all external links open in a new tab while keeping internal links unaffected. This approach streamlines link management without requiring manual adjustments for each link.

Rate this answer

Other Webflow Questions