"Open external links in a new tab by default" not working

Hello,

Lately, all the external links on my forum have stopped opening into a new tab. I’ve double-checked the option “open external links in a new tab by default” and it is ticked:

I checked the HTML behind the links via inspect element and target="_blank" is clearly missing.

What should I do?

1 Like

Have you tried this in safe-mode? Maybe a new theme, theme-component or plugin is interfering? :face_with_monocle:

2 Likes

I tried the site in safe mode but still the same issue

What version of Discourse are you running? It will be easier to see if I can replicate.

I’m running the latest 2.7.0.beta1

Can you test this out here on Meta? Set your preferences here to open external links in a new tab and see if it works for you. It works quite well for me.

1 Like

Hey @riteshsaini

One of your best first step troubleshooting options would be to examine the DOM directly using the web dev console; and to inspect what are the exact attributes of your link, in particular, the “target” attribute.

Often, you cannot see these DOM attributes directly reading the source code (these attributes can exist in the DOM but not in source), and so you will need to query the DOM to get the attributes (to be 100% certain).

Hope this makes sense.

2 Likes

I believe that is what he did here

No. Reading the source code is not the same as checking the DOM.

DOM attributes can be different (and often are) than the source code; especially on web sites which use a lot of Javascript to manipulate the DOM (like Discourse).

That is why I mentioned specifically to query the DOM and not simply look at the source code :slight_smile:

However, I agree with you @osioke, that the source code clearly shows the target attribute missing; but when we check the DOM, we will be “certain” what is the attribute; since the DOM can be in a different state than the source code (and often is in a different state).

Personally, I am a huge fan of “never assuming anything” so that is why I suggested that @riteshsaini “make sure” by checking the DOM and then “go from there”.

It is highly possible the DOM and the source code link target attributes are in the same state; but I always recommend folks troubleshooting problems not assume these kinds of details; hence my suggestion to query the DOM as a troubleshooting step.

Hope this helps.

2 Likes

Instead of tweaking in settings I use this way

Demo: Discourse Theme Creator

<script type="text/discourse-plugin" version="0.8">
    api.onPageChange((url, title) => {
        // Automatically open external links in new tab or window
        var pc = 1;
        if ($("html").hasClass("mobile-device")) pc = 0;
        var links = document.links;
        for (var i = 0, linksLength = links.length; i < linksLength; i++) {
            if (links[i].hostname != window.location.hostname) {
                if (pc) links[i].target = '_blank'; else links[i].target = '_self';
            }
        }
    });
</script>
2 Likes

… and illustrating perfectly in a solid Discourse API example how the DOM and the source code will be in a different state and you will not see the target attributes in the HTML source code set by the JS above :slight_smile:

2 Likes

@Bcat thanks for sharing this snippet. But where should I add it?

PS: sorry for the silly question. I’m not a coder and totally new to discourse :slight_smile:

1 Like

Hey @neounix thanks for your reply

I’m not really sure how to do that :slight_smile: enlighten me on that if you have some spare time.

1 Like

View topic: How do I install a Theme or Theme Component?

discourse-automatically-open-external-links-in-new-tab.zip (843 Bytes)

1 Like

Have you checked the settings on your personal profile under preferences/interface? Because usually this situation is created when on your personal profile the opening of external links in new tabs is disabled.

image

3 Likes

It was unticked, so I ticked it. Now it opens the links in the new tab only when I’m logged in. If I check the site in the incognito tab, it still does not work.

1 Like

It works as it should. Just perfectly! Thank you so much :slight_smile:

Any solution on how to add rel="nofollow" to all the external links, even those that are added by admin?

I’ve been strugging to do that from months now.

1 Like

For forums for the sharing and downloading using links out bitly or sites (less secure)
Would you consider using rel="noopener noreferrer" instead rel="nofollow"?
You can learn more about noopener noreferrer

1 Like

I can consider adding noopener noreferrer but I will still need to add nofollow along with them. The thing is, I don’t want Google to follow or index those links because they are affiliate links. Only nofollow can help with that.

1 Like

Add links[i].rel = 'nofollow'; or File: discourse-automatically-open-external-links-in-new-tab.zip (856 Bytes)

1 Like