Created
May 22, 2012 16:57
-
-
Save apphp-snippets/2770245 to your computer and use it in GitHub Desktop.
Sometimes you have some information on your page and your visitors might want to copy it. The easiest way is to provide a mechanism that allows them to simply click a button to do so. You have to paste this code into the head of your web page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script language="javascript"> | |
| /* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */ | |
| function CopyText(el){ | |
| var selectedText = ""; | |
| if(window.getSelection){ | |
| selectedText = window.getSelection(); | |
| }else if (document.getSelection){ | |
| selectedText = document.getSelection(); | |
| }else if (document.selection){ | |
| selectedText = document.selection.createRange().text; | |
| } | |
| if(selectedText != ""){ | |
| selectedText = selectedText.toString(); | |
| el.focus(); | |
| el.value = selectedText; | |
| }else { | |
| alert("Select a text in the page and then press this button!"); | |
| } | |
| } | |
| </script> | |
| Select any part of this text to copy it... | |
| <form name="frmCopyText"> | |
| <textarea name="txtSelect" rows="4" cols="45"></textarea><br> | |
| <input onclick="CopyText(this.form.txtSelect)" | |
| type="button" | |
| value="Press to copy the highlighted text" | |
| name="btnCopy"> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment