Adding "Read More" link to Copied Text from your Website?

hoangvu

New member
Joined
Jun 6, 2012
Messages
1,835
Points
0
How can I add "Read More link to Copied Text from your website?
Sometimes, when you need to retrieve content from a certain web page, copy and paste the content into notepad, the contents of which are usually attached to a link as Read more at: https://www.webmastersun.com/forums/21-Websites-Design . Website owners usually do this to protect content on their site as well as all copyright notices for people who want to write to get content.

Can you give me codes to do that? :eek:
 

Alex_smith

New member
Joined
Dec 19, 2012
Messages
385
Points
0
Interesting question :D

If you want to do that, you will use document.body.oncopy attribute, it returns onCopy event on the current element.

The syntax is as follows:

Code:
element.oncopy = functionRef;

We'll insert a different javascript function called window.getSelection (); returns an object representing the character sequence is selected by the user.

jQuery Code

Code:
$(document).ready(function () {
    document.body.oncopy = function () {
        var body_element = document.getElementsByTagName('body')[0];
        var selection;
        selection = window.getSelection();
        var pagelink = "<br />Read more at: <a href='" + document.location.href + "'>" + document.location.href + "</a><br />";
        var copytext = selection + pagelink;
        var newdiv = document.createElement('div');
        body_element.appendChild(newdiv);
        newdiv.innerHTML = copytext;
        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {
            body_element.removeChild(newdiv);
        }, 0);
    };
});
When you insert this code into your website, then, is from now on, anyone who copy content from your website, then along with the content that they want to take the section links pointing to your site also inserted them. I hope you'll enjoy with the code that I shared with you today.

Good luck!
 
Recommended threads
Similar threads
Top