/    Sign up×
Community /Pin to ProfileBookmark

Javascript code to copy and paste to a clipboard

<html>
<textarea name=”textarea” id=”ilink”>choose and copy contents</textarea>
<br /><br/>
<input type=”button” value=”Select Text” onClick=”SelectContents(ilink)”>

<a href=”javascript:SelectContents(‘ipwd’)”>Select and Copy Password</a>

<script language=javascript>
function SelectContents(ilink)
{
document.getElementById(“ilink”).select();
var copyrange=document.getElementById(“ilink”).createTextRange();
copyrange.execCommand(“Copy”);
}
</script>
<html>

the above code copies contents from the textarea when we click on the “selelct all” anchor tag. But sadly this code works only for IE browser.how to ensure that this works for mozilla also. thanks for any help.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@felgallOct 09.2008 — Browsers other than IE have NO ACCESS to the clipboard since it isn't part of your web page and the operating system may not even have such a thing.

You can enable it in the browser when you are using Firefox on an intranet but it is a security risk to do sofor the internet.
Copy linkTweet thisAlerts:
@rnd_meOct 09.2008 — Browsers other than IE have NO ACCESS to the clipboard since it isn't part of your web page and the operating system may not even have such a thing.

You can enable it in the browser when you are using Firefox on an intranet but it is a security risk to do sofor the internet.[/QUOTE]


your'e gonna love this one felgall!

it works in firefox (or any browser really) to [B]set [/B]the clipboard, no user config changes needed!

it is also nice that it requires no user confirmation or warning about the clipboard, simply sets the text.

big downside is that is limited to the amount of into it can handle. this varies by browser, but should always be at least 2kb.

thank you flash.


[CODE]
function copy(str) {
var D=document;
if(!copy.div) { copy.div = D.createElement('div'); D.body.appendChild(copy.div); }

flashVar = "Q1dTB3kAAAB4nKtgYI1nYOBfwMDAw8jgzPT//3975lAGBoYOdQYWhu
SczIKk/MSiFIac1Lz0kgyG4MriktRchuLUEme41DQmBg4GGRDJ6Cc0l4l BAibCzsDO
CDSJgwksyRwkzuAA5AIAd7oY/w==";

copy.div.innerHTML = '<embed src="'+flashVar+'" FlashVars="clipboard='+encodeURIComponent(str) +
'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
}

[/CODE]


use like: copy(document.getElmementById("ilink").value);





-----
one more thing (about the non-flash way):

i made the config change to unlock the clipboard in firefox a long time ago.

just change signed.applets.codebase_principal_support to true in about:config.

it's not a security risk (imho) because you still get a confirmation prompt whenever a site tries to get to the clipboard.

i have NEVER had the confirm box pop-up on a page i didn't expect it to.

if it ever did, i would simply click "no", and the javascript would get denied clipboard access.
Copy linkTweet thisAlerts:
@felgallOct 10.2008 — That is flash accessing the clipboard - not JavaScript.

It wouldn't work on ANY of the browsers on my computer because I have Flash turned off.

It also wouldn't work on any operating system that doesn't have a clipboard or where the browser doesn't support flash (which would include most browsers not running on a computer).
Copy linkTweet thisAlerts:
@swetha13authorOct 10.2008 — function copy(str) {

var D=document;

if(!copy.div) { copy.div = D.createElement('div'); D.body.appendChild(copy.div); }

flashVar = "Q1dTB3kAAAB4nKtgYI1nYOBfwMDAw8jgzPT//3975lAGBoYOdQYWhu

SczIKk/MSiFIac1Lz0kgyG4MriktRchuLUEme41DQmBg4GGRDJ6Cc0l4l BAibCzsDO

CDSJgwksyRwkzuAA5AIAd7oY/w==";

copy.div.innerHTML = '<embed src="'+flashVar+'" FlashVars="clipboard='+encodeURIComponent(str) +

'" width="0" height="0" type="application/x-shockwave-flash"></embed>';

}


can u pls explain the meaning of this code?

also which is the replacement statement of document.getelementbyid("ilink").

means which is ilink here?

also i changed the setting in about:config,but still the clipboard didnt work.
Copy linkTweet thisAlerts:
@swetha13authorOct 10.2008 — Hi,

in this link ,"http://developer.mozilla.org/en/Using_the_Clipboard",i found the following code:

const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].


getService(Components.interfaces.nsIClipboardHelper);


gClipboardHelper.copyString("Put me on the clipboard, please.");


but how do i implement this code in php?
Copy linkTweet thisAlerts:
@felgallOct 10.2008 — but how do i implement this code in php?[/QUOTE]

PHP is running on the server and there isn't a clipboard on the web server for it to access (there may be a clipboard on the computer itself but the web server software can't access it).
Copy linkTweet thisAlerts:
@rnd_meOct 10.2008 — That is flash accessing the clipboard - not JavaScript.

It wouldn't work on ANY of the browsers on my computer because I have Flash turned off.

It also wouldn't work on any operating system that doesn't have a clipboard or where the browser doesn't support flash (which would include most browsers not running on a computer).[/QUOTE]


I know it's not javascript, but a lot more people have flash (~90%) than a non IE browser (~35%).

didn't consider OS, what OS doesn't have a clipboard?



OP:

throw the copy function somewhere along side your other functions in the script.

then replace the function SelectContents with this new one:

[CODE]function SelectContents(ilink)
{
var elm = document.getElementById("ilink");
elm.select();
if(window.ActiveXObject){
var copyrange=document.getElementById("ilink").createTextRange();
copyrange.execCommand("Copy");
} else {
copy(elm.value);

}
}[/CODE]
×

Success!

Help @swetha13 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...