Add Source Link or Copyright Note When Someone Copies Text from your website

The following post shows a simple JavaScript code to add link for reference when someone copies text from your website.

You can just copy paste the following code to your webpage and add the link to the copied content.

 

$(document).on('copy', function(e) {
    var sel = window.getSelection();
    var url = window.location.href;
    var copyFooter = "<br /><br /><a href='" + url + "'>" + url + "</a>";
    var copyHolder = $('<div>', {
        html: getSelectionHtml() + copyFooter,
        style: {
            position: 'absolute',
            left: '-99999px'
        }
    });
    $('body').append(copyHolder);
    sel.selectAllChildren(copyHolder[0]);
    window.setTimeout(function() {
        copyHolder.remove();
    }, 0);
});

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

 

Follow the following video for complete guidance :