Skip to content

Commit

Permalink
Update external links attributes to open in a new tab (#12980)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramram-mf authored Nov 26, 2024
1 parent 162eb2f commit 2c91af3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions source/js/common/template-js-handles/external-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { set } from "react-ga";

/**
* ExternalLinks object for handling external links.
*/
const ExternalLinks = {
/**
* Initializes the ExternalLinks object.
*/
init() {
this.configure();
},
/**
* Set up the event handlers for the external links.
*/
configure() {
document
.querySelectorAll(
`a[href^="http"]:not([href*="${window.location.host}"]):not([href^="mailto:"])`
)
.forEach((link) => {
link.target = "_blank";
link.rel = "noopener noreferrer";
});
},
};

export default ExternalLinks;
2 changes: 2 additions & 0 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { initYouTubeRegretsRecommendationsPieChart } from "./foundation/pages/yo
import { initYoutubeRegretsCarousel } from "./foundation/pages/youtube-regrets/carousel";
import { initYoutubeRegretsLocomotiveScroll } from "./foundation/pages/youtube-regrets/locomotive-scroll";
import SiteNav from "./common/template-js-handles/site-nav.js";
import ExternalLinks from "./common/template-js-handles/external-links.js";

// Initializing component a11y browser console logging
if (process.env.NODE_ENV === "development") {
Expand All @@ -67,6 +68,7 @@ let main = {
Dropdowns.init();
FoundationCarousels.init();
SiteNav.init();
ExternalLinks.init();

this.fetchEnv((envData) => {
env = envData;
Expand Down

0 comments on commit 2c91af3

Please sign in to comment.