javascript 如何在所有外部链接上添加nofollow,并排除我提到的一些特定的网站网址

egmofgnx  于 2022-10-30  发布在  Java
关注(0)|答案(1)|浏览(175)

如何添加nofollow对所有外部链接,并排除一些特定的网站网址,我提到...在页脚我提到了我的参考链接,我的其他网站一样,由www.example.com供电developer.com我想排除这个链接,其他外部链接将nofollow。
我只想排除这个链接和其他外部链接将nofollow。

$(document).ready(function myFunction() {
var x = document.getElementsByTagName("a");
var i;
for (i = 0; i < x.length; i++) {
if (location.hostname!=x[i].hostname){
x[i].rel = "nofollow";
x[i].target = "_blank";
x[i].title = "Click to open in new window";
}

}
mft=setTimeout("myFunction()",0);
function LoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
}
else{
window.onload = function()
{
if(oldonload)
{oldonload();}
func();}}}
LoadEvent(function(){
myFunction();
});

});

i try this code but I don't know how to exclude specific url...
yjghlzjz

yjghlzjz1#

您可以在纯JavaScript中执行以下操作:向allowedHosts中添加更多主机以跳过它们。

// anonymous function to self contain variables
(()=>{
    // Exclude specific hosts
    const allowedHosts = [ location.host, 'duckduckgo.com', 'stackoverflow.com' ];

    function sanitizeLinks( root = document ){
        // Find all links within element
        const links = [ ...root.getElementsByTagName('a') ];

        // Add root to links if it is a link
        if ( root.nodeName === 'A' ) links.push( root );

        // Loop through all the links
        for ( const link of links ){

            // Skip if rel has been added
            if ( link.rel && link.rel.includes('nofollow') ) continue;

            let skipAddAttrs = false;

            // Check hosts if href is present on link
            if ( link.href ){

                // Loop through allowed hosts
                for ( const host of allowedHosts ){

                    // if link has allowed host skip adding attributes
                    if ( link.href.includes( host ) ){
                        skipAddAttrs = true;
                        break;
                    }
                }
            }

            if ( skipAddAttrs ) continue;

            // add attributes to not allowed links
            link.setAttribute('rel','nofollow');
            link.setAttribute('target','_blank');
            link.setAttribute('title','Click to open in new window');
        }
    } 

    // Observe document changes instead of using setTimeout / setInterval
    const documentObserver = new MutationObserver( ( mutationList, observer ) => {
        let runSanitize = false;

        // Loop through the observed changes
        for ( const mutation of mutationList ) {

            // Loop through any added nodes
            for ( const node of mutation.addedNodes ){
                const isHtmlAnchor = node.nodeName === 'A';
                const hasHtmlAnchor = node.getElementsByTagName('a').length > 0;

                // Skip if no anchor found
                if ( isHtmlAnchor == false && hasHtmlAnchor == false ) continue;

                // Run sanitize only when needed
                sanitizeLinks( node );
            }
        }
    });

    // start the observer and watch subtree changes
    documentObserver.observe( document.documentElement, { childList: true, subtree: true });

    // initial run
    sanitizeLinks();
})();

相关问题