# Copying text to the user's clipboard

Here's my code.

javascript:x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1)


Is this the correct way? Are there any flaws in it?

Running:

copy(1)


doesn't work by itself so that's not a possibility, and

javascript:x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1)


appears to be the best so far.

• Is the expected result for data to be copied to the clipboard without user action? – guest271314 Jan 29 '19 at 8:24
• Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted. – Toby Speight Jan 29 '19 at 8:54
• @guest271314 yes, after clicking the bookmarklet – not legit Jan 29 '19 at 16:16
• @TobySpeight It works, how would I give proof? – not legit Jan 29 '19 at 16:17
• Just edit to say how you've tested it, so that it's clear that it's not an off-topic question. BTW, does the code need to be all on a single line? It might be more readable if split across lines. – Toby Speight Jan 29 '19 at 16:22

javascript:(function(){x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1);})();

• Have you tried the code at the answer? Uncaught ReferenceError: copy is not defined – guest271314 Jan 30 '19 at 2:12