You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Copy To Clipboard Safari

Is it possible to copy the text from text area to clipboard in Safari (HTML), if yes then how, or if not, is there any alternate?

iMac, OS X Mavericks (10.9.4)

Posted on Jan 7, 2015 1:38 AM

Reply
8 replies

Jan 7, 2015 5:01 AM in response to QuickTimeKirk

Thanks again for the response 🙂

Here is my Code:


function CopyToClipboard () {

var input = document.getElementById ("text_area");

var textToCb = input.value;

var flag = true;

if (window.clipboardData) {

window.clipboardData.setData ("Text", textToCb);

}

else {

var forExecElement = CreateEelementCmd (textToCb);

SelectContent (forExecElement);

var supported = true;

try {

if (window.netscape && netscape.security) {

netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect"); }

flag = document.execCommand ("copy", false, null); }

catch (e) {

flag = false;

}

document.body.removeChild (forExecElement); }

if (flag)

{

alert ("The text is already in the clipboard");

}

else {

alert ("Your browser doesn't allow clipboard access!");

}



function CreateElementCmd (textToCb)

{

var ExecElement = document.createElement ("div");

ExecElement.style.position = "absolute";

ExecElement.textContent = textToCb;

document.body.appendChild (ExecElement);

ExecElement.contentEditable = true;

return ExecElement;

}

I hope it helps. In the above code, I am getting only the second alert i.e "Your browser doesn't allow clipboard access!". I have tried with all possible browsers but no luck.

Aug 17, 2015 6:02 PM in response to QuickTimeKirk

This is highly unhelpful. Where do you get 3 years? Using clipboardData is an HTML5 feature which is NOT an outdated approach. In fact it is still considered bleeding edge so there is not much information about caveats and how to overcome them. Browsers are still working to support this feature, as can be seen here:

http://caniuse.com/#feat=clipboard

Copy To Clipboard Safari

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.