JavaScript

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 :

Admin

Recent Posts

Build a simple JavaScript Calculator for basic arithmetic operations

In this article, we will build a simple calculator using JavaScript that can carry out…

4 days ago

Tech Bloggers Association organizing Blogging Competition for Women

In honor of Girls in ICT Day, the Tech Bloggers Association of Nepal (TBAN) proudly…

5 days ago

Navigating the Newsroom: The Impact of AI on Journalism

In the ever-evolving landscape of journalism, the emergence of Artificial Intelligence (AI) is reshaping traditional…

5 days ago

Hello Topik – The Ultimate Korean EPS Preparation App

App Created by Bishow Bhasa Campus Korean Language HOD, Mr. Thaneshwor Banjade with over 20…

5 days ago

Roundtable Highlights Nepal’s Software Export Potential: Challenges & Strategies

A recent roundtable discussion hosted by the South Asia Watch on Trade, Economics, and Environment…

1 week ago

Top 10 iPhone hacks you may not know

In the ever-evolving world of technology, our smartphones have become indispensable companions, particularly the iconic…

2 weeks ago

This website uses cookies.