To target=_blank or not to target=_blank, that is the question!

前端 未结 8 1880
野的像风
野的像风 2021-02-03 21:58

Should links to external sites set target=_blank? E.g. I am on www.acme.net and have a link to www.otherplace.net, should that link be:

相关标签:
8条回答
  • 2021-02-03 22:41

    It might also be worth to mention that using target attribute is not xhtml valid. I do usually open links in external window or tab because I see that most regular users (not the advanced ones) want it that way so that they can always get back to the site they were on - usually they would go deep into the other site and then it become unfriendly for them having to click back multiple times.

    So in terms of usability I think that there's more users that don't use special techniques to manually open links in new window/tab.

    With regards to the xhtml validation, you might want to decorate your links with rel="external" or some similar word then use this JS function to handle new window open. I did it like this 99% of time in the last few years.

    function externalLinks() {
        if (!document.getElementsByTagName) return;
        var anchors = document.getElementsByTagName("a");
        for (var i = 0; i < anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute("href") &&
           anchor.getAttribute("rel") == "external")
                anchor.target = "_blank";
        }
    }
    
    /**
        DOCUMENT LOAD
    **/
    $(document).ready(function () {
        /** 
            external links
        **/
        externalLinks();
    ....
    
    0 讨论(0)
  • 2021-02-03 22:51

    Some web idealists will state that you should allow the user to make their own choices when it comes to navigation - I have a lot of sympathy with this view. As web developers, we shouldn't be forcing such decisions on our visitors.

    However, I also know that businesses often want to 'retain control' and so insist on spawning a new tab/window for external sites. An I understand this too - It's a very practical approach, particularly when you consider that how many users don't know how to control their own UA.

    I often tend to steer a middle course between the two, by adding an image (I'm sure you will have seen many in your time) that indicates which links are external, and a note to indicate that external links will open in a new tab/window.

    Not quite as 'pure' as the first option, but at least it is clear to the user how the site will behave.

    0 讨论(0)
提交回复
热议问题