6

In a huge table of numbers, I made the user's experience "richer" by replacing all of the semi-visible minus signs by –. It looks great, big improvement. I was so busy admiring my cleverness that I forgot to notice the blood on the floor.

Because, come to find out, when the guy goes to select, copy, and then paste (elsewhere) such transformed minus signs, guess what? they're not minus signs any more.

Can I use the onCopy event dependably, straightforwardly, and cross-browserly (including Mac browsers) to change those – chars back to minus signs in the matter that was (or is about to be) copied?

If so, have you any hints on doing it?

EDIT: I'm using native JavaScript, not using any framework.

Thanks!

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51

2 Answers2

1

I don't believe there's a way JavaScript can manipulate what is inside the clipboard because that's an OS feature. What you could do I believe is manipulate the text after the user pastes it into a field of your choosing. Here's an example with JQuery:

$('#my_text_field').bind('paste',function()
{
    $(this).val($(this).val().replace('–','-'));
}
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • thanks, but I'm not using jQuery. Have you some native JS solution? – Pete Wilson Jul 08 '11 at 00:39
  • 1
    "dependably, straightforwardly, and cross-browserly (including Mac browsers)" != native js :) – AlienWebguy Jul 08 '11 at 00:41
  • The user is pasting the stuff someplace else, like into a spreadsheet. – Pete Wilson Jul 08 '11 at 00:48
  • 1
    I don't know how they do it, but some news websites definitely manipulate the copy process: select some text on one of their pages and copy to the clipboard and what ends up in the clipboard is the selected text plus a short reference to the site (perhaps their URL). Not sure how cross-browser that functionality is. – nnnnnn Jul 08 '11 at 01:01
  • I hear you can do it with Flash , but not with JavaScript. – AlienWebguy Jul 08 '11 at 01:04
1

The – character will copy/paste correctly if inserted programmatically using javascript (and if you insert the actual character, instead of the HTML entity).

Perhaps you could replace every – that you have with something like:

<span class="fancyDash"></span>

And then on load you can run something like:

var longDash = '\u2013';
jQuery.each($(".fancyDash"), function() {
    this.innerHTML = longDash;
});

Here is a working example: http://jsfiddle.net/m9fhS/

Edit:

Or, if not using jQuery, you can first patch up document.getElementsByClassName so that it works correctly for anyone using IE, and then do:

var longDash = '\u2013';
var spans = document.getElementsByClassName("fancyDash");
for (var index = 0; index < spans.length; index++) {
    spans[index].innerHTML = longDash;
}

As shown here: http://jsfiddle.net/m9fhS/1/

aroth
  • 54,026
  • 20
  • 135
  • 176