Copy text to clipboard when clicking a link

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Copy text to clipboard when clicking a link

Post by vigge89 »

I need a script that, when a link is clicked, copies a text echoed by a php scipt. I dont know javascript, so i have no idea, but it would look something like this:

php has echoed a string in the script which says what to copy => user clicks the link => javascript copies the string from the php-script into clipboard => a popup pops up saying that the text is copied.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

hey! i tried that not too long ago. try searching for me. there's the one i did on that, and the answeri got, and a diff thread i commented in., both of which should be what you are looking for. i'll tyr to find them to help you
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

m3rajk wrote:hey! i tried that not too long ago. try searching for me. there's the one i did on that, and the answeri got, and a diff thread i commented in., both of which should be what you are looking for. i'll tyr to find them to help you
thanks :D
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

my post: viewtopic.php?t=13511
what i hacked up in that try: viewtopic.php?t=13378
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

thx, ill take a look at them now :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

hmmmmm, i didn't found it there so ill ask it here:
is it possible to make it crossbrowser?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

as a client side function it can't be done for two reasons:

1: javascript has no way to copy. only M$IE's version (jscript) which has a s**tload of non-standards, does. this willONLY WORK IN M$IE 5.0 (maybe 5.5) AND HIGHER

2: the aformentined jscript will only work with a form, when the button get the info from a textarea or text field
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, then i just skip the copy to clipboard prt onmy site :?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

lol. that was my reaction too. would you like to see what i wrote figuring that i'll play with it after i get the site online to see if i can get it working properly?

maybe working together we can find something that'll get around M$SE's screwball antics and do what we want.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

m3rajk wrote:lol. that was my reaction too. would you like to see what i wrote figuring that i'll play with it after i get the site online to see if i can get it working properly?

maybe working together we can find something that'll get around M$SE's screwball antics and do what we want.
well, ok, but i cant help you on the jscript coding, but ill help ya with anything else, just ask ;)
moerlien
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2003 12:20 pm

Post by moerlien »

Here's a link to a free code script that might be useful. They are working on a beta version that will be cross browser.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

vigge89 wrote:
m3rajk wrote:lol. that was my reaction too. would you like to see what i wrote figuring that i'll play with it after i get the site online to see if i can get it working properly?

maybe working together we can find something that'll get around M$SE's screwball antics and do what we want.
well, ok, but i cant help you on the jscript coding, but ill help ya with anything else, just ask ;)
lol.

here's what i have:

Code: Select all

<script language="Javascript">
function toClipboard(){
  var text="/* text to copy goes here */";
  var copyText=text.createTextRange();
  
  if(confirm("Do you want to copy the link /* here i double the copy text */ to your clipboard?\n(this only works for M\$IE");){
    copyText.execCommand("Copy");
    alert("If using M\$IE, then you have just copied the link, /* again i give the code to be copied */ to your clipboard.");
  }
}
</script>
Last edited by m3rajk on Tue Oct 28, 2003 10:18 pm, edited 1 time in total.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

what does that smilie do there? Nah, j/k it's ; ) ;)
Ok that works on M$IE, but not on NS or anyone else (yet :? ).
Post Reply