Clipboard - Cut Copy Paste With JavaScript

Discussion in 'Javascript' started by Mahsha, Jun 1, 2009.

  1. Mahsha

    Mahsha Guest

    <html>
    <head>
    <title>Clipboard cut, copy and paste with JavaScript</title>
    <script type="text/javascript">
    function CutToClipboard()
    {
    CutTxt = document.selection.createRange();
    CutTxt.execCommand("Cut");
    }

    function CopyToClipboard()
    {
    CopiedTxt = document.selection.createRange();
    CopiedTxt.execCommand("Copy");
    }

    function PasteFromClipboard()
    {
    document.Form1.txtArea.focus();
    PastedText = document.Form1.txtArea.createTextRange();
    PastedText.execCommand("Paste");
    }
    </script>
    </head>
    <body>
    <form name="Form1">
    <h1>Clipboard cut, copy and paste with JavaScript</h1>
    Select this text, copy it using the copy button, and paste it
    below.<br /><br />
    <textarea id="txtArea" cols="60" rows="5"></textarea>
    <br />
    <input type="button" onClick="CutToClipboard()" value="Cut to
    clipboard" />
    <input type="button" onClick="CopyToClipboard()" value="Copy to
    clipboard" />
    <input type="button" onClick="PasteFromClipboard()" value="Paste
    from clipboard" />
    </form>
    </body>
    </html>
    Mahsha, Jun 1, 2009
    #1
    1. Advertising

  2. Mahsha wrote:
    > <html>


    The DOCTYPE declaration is missing again.

    <http://validator.w3.org/>

    > <head>
    > <title>Clipboard cut, copy and paste with JavaScript</title>
    > <script type="text/javascript">


    At last.

    > function CutToClipboard()


    It's not a constructor, so it should not start uppercase.

    > {
    > CutTxt = document.selection.createRange();


    Undeclared (and unwise) identifier, missing feature test.

    > CutTxt.execCommand("Cut");
    > }
    >
    > function CopyToClipboard()
    > {
    > CopiedTxt = document.selection.createRange();
    > CopiedTxt.execCommand("Copy");
    > }
    >
    > function PasteFromClipboard()


    See above.

    > {
    > document.Form1.txtArea.focus();


    document.forms["Form1"].elements["txtArea"].focus()

    > PastedText = document.Form1.txtArea.createTextRange();
    > PastedText.execCommand("Paste");
    > }


    but in this case

    function pasteFromClipboard(f, controlName)
    {
    /* Add feature tests here! */
    var pastedText = f.elements[controlName].createTextRange();
    pastedText.execCommand("Paste");
    }

    <input type="button" value="Paste from clipboard"
    onclick="pasteFromClipboard(this.form)">

    Although it is the first time I see execCommand() called as method of
    a form control -- always thought it was document.execCommand() only,
    and you had to focus the control first.

    > </script>
    > </head>
    > <body>
    > <form name="Form1">


    The #REQUIRED `action' attribute is missing.

    > <h1>Clipboard cut, copy and paste with JavaScript</h1>


    Better.

    > Select this text, copy it using the copy button, and paste it
    > below.<br /><br />


    <p>Select this ...<p>

    instead.

    > <textarea id="txtArea" cols="60" rows="5"></textarea>


    The textarea needs a _name_ if this should work everywhere.

    > <br />


    Probably a block element, say `div', enclosing the rest would be better.

    > <input type="button" onClick="CutToClipboard()" value="Cut to
    > clipboard" />
    > <input type="button" onClick="CopyToClipboard()" value="Copy to
    > clipboard" />
    > <input type="button" onClick="PasteFromClipboard()" value="Paste
    > from clipboard" />


    At last. Now you only have to generate them dynamically (as they don't work
    without scripting), and either get rid of the `/' or write proper XHTML
    (including `onclick' instead of `onClick'). One probably wants to try the
    `button' element here, and icon images on/in it.


    PointedEars
    Thomas 'PointedEars' Lahn, Jun 1, 2009
    #2
    1. Advertising

  3. Mahsha meinte:

    [snip]

    Ah, another round in Masha's quiz show "Guess the question".

    Are you showing off your JS "abilities" or do you need help? In the
    latter case, it would be nice to elaborate on what you want.

    Gregor
    Gregor Kofler, Jun 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    custom cut copy and paste

    =?Utf-8?B?QW5nZWw=?=, Jan 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    =?Utf-8?B?QW5nZWw=?=
    Jan 10, 2005
  2. tk
    Replies:
    0
    Views:
    841
  3. Stephen Boulet

    Cut and paste to X clipboard?

    Stephen Boulet, Jun 25, 2004, in forum: Python
    Replies:
    3
    Views:
    529
    Stephen Boulet
    Jun 29, 2004
  4. Replies:
    0
    Views:
    297
  5. Replies:
    4
    Views:
    256
    Randy Webb
    Dec 14, 2004
Loading...

Share This Page