The article URL code is automatically added after the content is copied.

Source: Internet
Author: User

We want to automatically add article links to the copy when copying content. There are many methods to implement js, jquery, and flash. Below I will introduce wordpress as an example.

Method

You can add a function to your WordPress site: After the content is copied, the link of the post is automatically added when it is pasted.

Add the following code to the topic's functions. php file:

The Code is as follows: Copy code

Function add_copyright_text (){
If (is_single () {?>
 
<Script type = 'text/javascript '>
Function addLink (){
If (
Window. getSelection (). containsNode (
Document. getElementsByClassName ('entry-content') [0], true )){
Var body_element = document. getElementsByTagName ('body') [0];
Var selection;
Selection = window. getSelection ();
Var oldselection = selection
Var pagelink = "<br/> Read more: <? Php the_title ();?> <A href = '<? Php echo get_permalink (get_the_ID ();?> '> <? Php echo get_permalink (get_the_ID ();?> </A> "; // modify this line of code as required.
Var copy_text = selection + pagelink;
Var new_div = document. createElement ('div ');
New_div.style.left = '-99999px ';
New_div.style.position = 'absolute ';
 
Body_element.appendChild (new_div );
New_div.innerHTML = copy_text;
Selection. selectAllChildren (new_div );
Window. setTimeout (function (){
Body_element.removeChild (new_div );
}, 0 );
}
}

Document. oncopy = addLink;
</Script>
 
<? Php
}
}
 
Add_action ('wp _ head', 'add _ copyright_text ');


If you do not want to modify the PHP file, we can directly implement it using js without adding the above Code.

The Code is as follows: Copy code


<Script type = "text/javascript">
Document. body. oncopy = function (){
Settimeout (function ()
{
Var text = clipboarddata. getdata ("text ");
If (text ){
Text = text + "rn this article comes from [Left Bank] (http://www.bKjia. c0m) Original Source:" + location. href;
Clipboarddata. setdata ("text", text );
}
}, 100)
}
</Script>

For more details, see: http://www.bKjia. c0m/wy/js-ajax/41045.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.