Copy the content of the Web page, paste it automatically after the implementation of the URL (script home special collation) _javascript tips

Source: Internet
Author: User

Did not think of an unintentional function, has now been passed by everyone so violent, think we released after the next day to find many sites have added this function, a little regret, or we will be more outside the chain. Of course there is disgust, the tool is good to see the people.

Today's search found that there are many friends like this function, where the cloud-Habitat Community Small series to continue to be a feature of the version, you can choose to use according to need.

Open some websites, when we browse certain articles, feel very good, so often the articles of these websites are copied;

Later, paste into Word or Notepad, save, in the implementation of the "copycat" at the same time, the "account for their own"!
If you are a small bee like the webmaster, every day hard-working, and daily like the old cattle as hard, then, when your site content by others for unwarranted possession but did not specify the source address of the article, I think you will fire up!
Therefore, in order to avoid this situation, many webmasters do so!
Add such a function to the Web page, when others copy the content of the site, paste into Word or notepad and other software inside, will automatically add the site address or the source of the detailed address of the article!
If you also encounter this situation, please study with me, how to do this effect!

Implementation method:

The method is very simple, the following code can be used!

Copy Code code as follows:

<script type= "Text/javascript" >
Document.body.oncopy = function () {
settimeout (function () {
var text = clipboarddata.getdata ("text");
if (text) {
Text = text + \ r \ n Article from: Website name (http://www.jb51.net) detailed text reference: "+LOCATION.HREF; Clipboarddata.setdata ("text", text);
}
}, 100)
}
</script>

Add the above code, copy, and then paste the corresponding page into your site will be available!

Note: This code is generally placed in the <body> area, or there is a choice of place to do it!

Knowledge Extension:

Although the above code can prevent replication after automatically add URL, but, not flexible, later to modify or make any changes, it is necessary to modify such code is not, then, we can use in the page to introduce a JS file method to achieve flexible functions!

Open Notepad, and then copy the following inside to paste (this code is similar to the above code, but not the same, please note):

Copy Code code as follows:

Document.body.oncopy = function () {
settimeout (function () {
var text = clipboarddata.getdata ("text");
if (text) {
Text = text + "\ r \ n Article from: Cloud Habitat Community (http://www.jb51.net) article address:" +LOCATION.HREF; Clipboarddata.setdata ("text", text);
}
}, 100)
}

After that, save it as a xx.js file and save it in the appropriate directory on your site!

Finally, you can introduce the Xx.js file on the page, such as the following, for the reference sample code:

<script src= "Datafiles/java/addmyurl.js" type= "Text/jscript" ></script>

The above red color code, is the reference sample code!

Note, the introduction of the code, generally placed behind the content of the article, before </body>!

Our above code is through JS to get the contents of the Clipboard, and the following is through JS to obtain the selection of content, the function is the same. But not much of this.

<ptml> <pead> <title> Copy content automatically add your URL information </title> </pead> <body> <script> doc Ument.body.oncopy=function () {event.returnvalue=false; var s=document.selection.createrange (). text; t = S+ ' \ n This article to cloud-dwelling community http://www.jb51.net '; Clipboarddata.setdata (' Text ', t); </script> This text is purely a test, you can copy it, and then paste it to other places, see, is not inside the site to join the website. </body> </ptml></P> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Under the cloud Habitat Community Small series for everyone to provide a copy of the content after the middle of the domain name, find more difficult.

Document.body.oncopy=function ()
{ 
settimeout (function () { 
var d = clipboarddata.getdata ("text"); 
var lenstr = d.length;
var leftnum = Math.floor (LENSTR/2);
var rightnum = lenstr-leftnum;
var addstr = "(www.jb51.net)"; 
var AddStr2 = "From:" +LOCATION.HREF;
if (Lenstr >)
{newcontent = left (d,leftnum) +addstr+ Right (D,rightnum) +addstr2;}
Else{newcontent = D;}
Clipboarddata.setdata ("Text", newcontent);
}

The above copy of the content of the Web page, after pasting automatically add Web site introduction on the first to this, later found better for everyone to provide.

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.