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

前端 未结 8 1884
野的像风
野的像风 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();
    ....
    

提交回复
热议问题