How can I make links open in a new tab

I’ve tried to put these three bing AI generated codes on the ghost header, I’ve also tried putting them in the ghost footer, saved, and nothing happened.

these were the codes:

  • <script type=“text/javascript”> var links = document.querySelectorAll(‘a’); links.forEach((link) => { var a = new RegExp(‘/’ + window.location.host + ‘/’); if(!a.test(link.href)) { link.addEventListener(‘click’, (event) => { event.preventDefault(); event.stopPropagation(); window.open(link.href, ‘_blank’); }); } }); </script>
    * <script type=“text/javascript”> var links = document.getElementsByTagName(“a”); for (var i = 0; i < links.length; i++) { if (links[i].hostname != window.location.hostname) { links[i].target = ‘_blank’; } } </script>
  • <script type=“text/javascript”> var links = document.querySelectorAll(‘a’); links.forEach((link) => { if (link.href.includes(‘?xgtab&’)) { link.target = ‘_blank’; } }); </script>

Also, this code that I’ve seen in another forum question in this link: I want my external links to open in a new tab - Developer help - Ghost Forum

Which was this

<script type='text/javascript'>

$( document ).ready(function() {

$(".post-content a").attr("target","moredetail");

});

Then I tried this one

<script>
	var links = document.querySelectorAll('a');
	links.forEach((link) => {
		var a = new RegExp('/' + window.location.host + '/');
		if(link.href.indexOf("javascript") == -1){
			if(!a.test(link.href)) {
				link.addEventListener('click', (event) => {
					event.preventDefault();
					event.stopPropagation();
					window.open(link.href, '_blank');
				});
			}
		}
	});
</script>

Which works, but only for external links

But I need it to open both internal and external links on new tab
how can I do it?
my website is Panther Blog (pantherprotocol.io)

Update:
Slowly, I’ve come up with a code that does it for me
which is this one:

<script>
var links = document.querySelectorAll('a');
links.forEach((link) => {
  if (link.href.indexOf("javascript") == -1 && link.href != "https://blog.pantherprotocol.io/#/portal/signup") {
    // check if the link is not a javascript function and not the signup link
    link.addEventListener('click', (event) => {
      event.preventDefault();
      event.stopPropagation();
      window.open(link.href, '_blank');
    });
  }
});
</script>

and it works perfectly, but only with the previously loaded blog articles. When I scroll further into the page, it doesn’t work with the articles that weren’t previously loaded. How can I solve this?