Home > Blockchain >  how to add nofollow on all external link and exclude some specific website url that i mentioned
how to add nofollow on all external link and exclude some specific website url that i mentioned

Time:10-30

how to add nofollow on all external link and exclude some specific website url that i mentioned... In footer i mentioned my reference link to my other website like powered by developer.com I want to exclude this link only and other external link will be nofollow.

I want to exclude this link only and other external link will be 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...

CodePudding user response:

You could do the following in plain javascript. Add more hosts to the allowedHosts to skip over them.

// 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();
})();
  • Related