Skip to content Skip to sidebar Skip to footer

How Can I Add Rel = "nofollow" To A Link In Ckeditor If It's An External Link

i want to give rel='nofollow' to my external links which its content managed by ckeditor. example.com = my site externallink.com = any external link For example:

Lor

Solution 1:

This solution also works in Internet Explorer:

CKEDITOR.on('instanceReady', function(ev) {
    var editor = ev.editor;
    editor.dataProcessor.htmlFilter.addRules({
        elements : {
            a : function( element ) {
                if ( !element.attributes.rel ){
                    //gets content's a href valuesvar url = element.attributes.href;

                    //extract host names from URLs (IE safe)var parser = document.createElement('a');
                    parser.href = url;

                    var hostname = parser.hostname;
                    if ( hostname !== window.location.host) {
                        element.attributes.rel = 'nofollow';
                        element.attributes.target = '_blank';
                    }
                }
            }
        }
    });
})

Solution 2:

I solved it like;

CKEDITOR.on('instanceReady', function(ev) {
        var editor = ev.editor;
        editor.dataProcessor.htmlFilter.addRules({
                elements : {
                    a : function( element ) {
                        if ( !element.attributes.rel ){
                           //gets content's a href valuesvar url = element.attributes.href;
                           //extract host names from URLs var hostname = (newURL(url)).hostname;
                            if ( hostname !== window.location.host && hostname !=="youranothersite.com") {
                                element.attributes.rel = 'nofollow';
                            }
                        }
                    }
                }
            });
    })

Solution 3:

So you need to compare hosts, something like this should work.

a : function( element )
    {
        if ( element.host !== window.location.host ) {
            element.attributes.rel = 'nofollow';
        }
    }

Post a Comment for "How Can I Add Rel = "nofollow" To A Link In Ckeditor If It's An External Link"