Open internal links in same browser tab

Many of us put discourse in a subdomain and also have a site in the root, eg, meta.discourse.org and discourse.org. However when the global setting of default other external links in new tab is set to Open external links in a new tab by default it assumes all non-forum links are external - is there any way to add domains so that they open in the same browser tab? (If not, could this option be added please?)

There is an allowed internal hosts setting, with desc: A list of internal hosts that discourse can safely crawl for oneboxing and other purposes - maybe simply use this and domains added here could set them to open in the same tab?

1 Like

This sounds like a very specific need, so your best bet is doing it via theme/plugin.

2 Likes

I’d want to do this on every Discourse instance I run tbh Rafael, I can’t see why anyone wouldn’t want to do it - if a site is the same internal/parent domain you’d probably want the link to open in the same window. I wonder if it’s worth putting up a poll for things like this to get a feel for what people using Discourse might prefer? I think you might be surprised by the results :blush:

What’s the best way to do this Rafael?

I tried using:

<script type="text/discourse-plugin" version="0.8.42">
	api.decorateCookedElement(
	  element => {
	    const tags = element.querySelectorAll('.hashtag');

	    tags.forEach(function(tag) {
				if (tag.hasAttribute('href')) {
					if (tag.href.includes("site.com/tag/")) {
						tag.href = tag.href.replace("site.com/tag/", "newsite.com/")
						tag.setAttribute("target", "_self");
					}
				}
	    });
    },
	  {
	    onlyStream: true
	  }
	);
</script>

and while:

tag.setAttribute("target", "_self");

sets the attribute it doesn’t not have any impact, with links opening in a new tab as normal.

I guess I need to use something other than api.decorateCookedElement?