פתח קישורים פנימיים באותה לשונית דפדפן

סיכום מבוסס בינה מלאכותית

In a Support category discussion titled “Open internal links in same browser tab,” AstonJ raises a common issue: when Discourse is hosted on a subdomain (e.g., meta.discourse.org) alongside a root domain site (e.g., discourse.org), the setting “Open external links in a new tab by default” treats all non-forum links as external—even if they’re on the same parent domain. AstonJ suggests leveraging the existing “allowed internal hosts” setting to determine which domains should open in the same tab, proposing this as a universally useful feature. Falco responds that this is a niche need best handled via theme/plugin. AstonJ disagrees, arguing it’s broadly desirable and suggests a poll to gauge community preference. In post 4, AstonJ shares a failed attempt to override link behavior using api.decorateCookedElement and target="_self", noting the attribute is set but ignored. They suspect a different API method is needed. The core issue remains unresolved: how to make internal domain links (e.g., same parent domain) open in the same tab without requiring custom code.

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?

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

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?