javascript help ''copy to clipboard'' button

Started on the old forum in November 2013 · view the original in the archive · old links redirect here

6 replies · 403 views · started 13 years ago · 1 min read

3 participants
metulj Super moderator Principal · 1,726 posts #1

I got not much clue about javascript, so i need help.

I get some result in PHP, something like this:

echo "Here is your result:<br/><input type='text' size='50' value='$final_result'>";
///// Need copy to clipboard BUTTON here for value $final_result

I've found some code on google, but got no clu how to use it.

<script language="JavaScript">
<!-- Begin
function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
}
//  End -->
</script>
<form name="aspinc">
<pre>
<div align="center">
<input onclick="copyit('aspinc.select1')" type="button" value="Highlight All" name="cpy">
<p>
<textarea class="codebox-sm" name="select1">
Text you want to highlight goes here.
</textarea>
</p>
</pre>
</div>
</form>

where and how to place this javascript ?

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

GumSlone Administrator Principal · 1,690 posts #2

javascript can be placed directly into the html document, btw. js copy to clipboard doesnt work in all browsers, there are advanced solutions which work with flash, but flash is not supported by mobiles

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

metulj Super moderator Principal · 1,726 posts #3

so... theres no proper solution to ''satisfie'' all browsers ? it'll have to javascript then, as mobile browsers are more important to me is there any better solution than posted above ?

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

something else Fellow · 2,296 posts #4

I think with javascript copy functions it is impossible to do unless you have browser permissions (such permissions you can get on addons only) - i could be wrong.

Most people end up using flash copying programs such as zero clipboard to get around the problem.

but you can try as i maybe wrong and not up to date with browsers..... something like this:

<html>
<head>
<script language="JavaScript">
<!-- Begin
function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
}
//  End -->
</script>
</head>

<?php
echo '<form name="yourFormName">';
echo "Here is your result:<br/><input type='text' size='50' value='$final_result' name='inputName'>";
echo "<button onclick=\"copyit('yourFormName.inputName');\">Copy</button>";
echo "</form>";
?>

Added after 18 minutes:

by the looks of things this would only work in IE and safari 🙁 for mobiles its a no 🙁 - closest thing you could do is highlight all via javascript for users to press the copy button on there phones - making it slightly less of a task

metulj Super moderator Principal · 1,726 posts #5

something else wrote: I think with javascript copy functions it is impossible to do unless you have browser permissions (such permissions you can get on addons only) - i could be wrong.

Most people end up using flash copying programs such as zero clipboard to get around the problem.

but you can try as i maybe wrong and not up to date with browsers..... something like this:

<html>
<head>
<script language="JavaScript">
<!-- Begin
function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
}
//  End -->
</script>
</head>


<?php
echo '<form name="yourFormName">';
echo "Here is your result:<br/><input type='text' size='50' value='$final_result' name='inputName'>";
echo "<button onclick=\"copyit('yourFormName.inputName');\">Copy</button>";
echo "</form>";
?>

Added after 18 minutes:

by the looks of things this would only work in IE and safari 🙁 for mobiles its a no 🙁 - closest thing you could do is highlight all via javascript for users to press the copy button on there phones - making it slightly less of a task

exactly, LOL it works, but only in IE, didnt even tried in safari 🙂 I ned some better solution

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

something else Fellow · 2,296 posts #6

havent tried this on mobile but here is how i do it on chrome extensions to highlight text for copying:

<input type="text" onclick="this.focus();this.select();" />
metulj Super moderator Principal · 1,726 posts #7

something else wrote: havent tried this on mobile but here is how i do it on chrome extensions to highlight text for copying:

<input type="text" onclick="this.focus();this.select();" />

Well, this works even on android default browser, so it kind a better solution then the first one 🙂 Thanks

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close