Reputation: 1295
i am wondering if it is possible in javascript/jquery to copy the current url and paste it to the clipboard using pure js.
this is mainly for a phone eg iphone running safari i want to make a button that will go and get the url and have it in the clipboard so the user could then open their messages for example and paste it in?
Is this even possible?
I have seen zclip but this requires flash which obviously will not work on a apple device
cheers
Upvotes: 1
Views: 2351
Reputation: 934
Yes you cant acess user clipboard using only javascript but most browsers make it very difficult. Example in IE is clipboardData object, in FF4+ you have window.ClipboardEvent in chrome window.Clipboard, but you must check about diffrent version of browser. Flash in this task is bit more better. I recommend two ways: flash way Show user prompt and select text that you want to that user copy or only use focus and select combination for input text:
function toClipboard(txt) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", txt);
}
or
$('input').focus().select();
As you know you can get actual url by accessing window.location.href property.
Upvotes: 1