tags:

views:

87

answers:

3

I was just reading the Times online and I wanted to copy a bit of text from the article and IM it to a friend, but I noticed when I did so, it automatically appended the link back to the article in what I had copied.

This is not a feature of my IM client, so I assume this happened because of some javascript on Times website.

How would I accomplish this if I wanted to implement it on my site? Basically, I would have to hijack the copy operation and append the URL of the article to the end of the copied content, right? Thoughts?

Here's the article I was reading, for reference: http://www.time.com/time/health/article/0,8599,1914857,00.html

A: 

What browser are you using (and what version)?

In some newer browsers, the user is either asked if a website can access the clipboard, or its simply not allowed. In other browsers (IE 6, for example), it is allowed, and websites can easily read from and write to your copy clipboard.

Here is the code (IE only)

clipboardData.setData("Text", "I just put this in the clipboard using JavaScript");
Gabriel McAdams
Safari 4 doesn't ask permission. It restricts clipboard access to copy and paste events when appropriate.
eyelidlessness
I tried on FF and I got the website appended at the end as well.
Anthony Forloney
I was using Safari 4 and I wasn't asked. How would I append data to what the user selects? You example sets the data explicitly... thanks!
neezer
You can also read the clipboard by using clipboardData.getData (these functions, though, are IE only)
Gabriel McAdams
Here is another method that I have not used. It may work on other browsers: http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html
Gabriel McAdams
+2  A: 

It's a breeze with jQuery (which your referenced site is using):

$("body").bind('copy', function(e) {
    // The user is copying something
});

You can use the jQuery Search & Share Plugin which does this exact thing whenever somebody copies more than 40 chars from your site: http://www.latentmotion.com/search-and-share/

The site that you referenced is apparently using a service called Tynt Insight to accomplish this though.

Jonathan Sampson
Does the jQuery copy event allow you to alter the content?
Matchu
Matchu, the event doesn't. It merely gives you an alert of when you should alter the content. There is a jQuery plugin that does all of this for you though, I'll reference it in my answer.
Jonathan Sampson
Tynt is all the rage lately (in all the senses of the word).
Justin Johnson
Excellent! Just what I was looking for. Thanks!
neezer
+1  A: 

They are using the free service Tynt. If you want to accomplish the same thing, just use the same service.

Matchu