Skip to content

Instantly share code, notes, and snippets.

@binsarjr
Created June 2, 2022 00:27
Show Gist options
  • Save binsarjr/acaf6cad60debe85f71e66d0568dd90b to your computer and use it in GitHub Desktop.
Save binsarjr/acaf6cad60debe85f71e66d0568dd90b to your computer and use it in GitHub Desktop.

Auto target blank when difference host

document.addEventListener("click", function (e) {
  if (!e?.target?.href) return
  if (new URL(e.target.href).host != new URL(window.location.href).host && !e.target.hasAttribute("target")) {
    e.preventDefault();
    e.target.setAttribute("target", "_blank");
    window.open(e.target.href,'_blank')
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment