dcsimg
www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Is there a way to simulate CTRL-A / CTRL-C in javascript?

  1. #1
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347

    Is there a way to simulate CTRL-A / CTRL-C in javascript?

    Just curious. I've seen how to copy stuff to the clipboard from a textarea, but what if I want to copy the whole page?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    The clipboard isn't part of the web browser so Javascript doesn't have access to it (except in Internet Explorer).

  3. #3
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    Well, we're an IE only shop...so that's okay.

  4. #4
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    bump

  5. #5
    Join Date
    Aug 2004
    Location
    Edinburgh
    Posts
    244
    Originally posted by TomWeinstock
    we're an IE only shop...so that's okay.
    well, is it okay, tho? for a website, I'd say that it isn't. furthermore, for a website I'd say that having access to a user's clipboard also isn't ok. that's why it's not accessible in FF nor is it accessible by default in IE anymore (SP2 put paid to that).

    as far as simulating the keystrokes, I don't know that it is possible anyway in any browser.

  6. #6
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    It's not for a website...they're reports that are precompiled HTML pages. I want the user to be able to click a button/link that will copy the contents a table to clipboard so that it can be pasted into Excel/e-mail, etc.

  7. #7
    Join Date
    Mar 2005
    Posts
    121
    If it were for an internal site (LAN) developed and used as a tool, and not an external (WAN) public-access site, I don't see why there would be anything wrong allowing that kind of access to the clipboard. Am I missing something? It's totally possible cuz I'm still a noob at this too.

  8. #8
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    The reports are internal only and are hosted on our intranet.

  9. #9
    Join Date
    Aug 2004
    Location
    Edinburgh
    Posts
    244
    Originally posted by TomWeinstock
    I want the user to be able to click a button/link that will copy the contents a table to clipboard so that it can be pasted into Excel/e-mail, etc.
    then write some ActiveX which will parse the content straight into those apps. it's easier than you think. visit msdn.microsoft.com/activex to find out more.

  10. #10
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    thanks jbot...I'll have a look.

  11. #11
    Join Date
    Mar 2005
    Posts
    121
    You may already know this, but the .execCommand("SelectAll") and .execCommand("Copy") do work inside a function to perform the said actions, but I don't know if you can get them to select the whole page. I can get them to select a whole textbox. Ex.

    function ClipBoard2()
    {
    Copied = editarea.createTextRange();
    Copied.execCommand("SelectAll");
    Copied.execCommand("Copy");
    }

    Of course, for this command, Copied.execCommand("SelectAll"); isn't necessary, but just there as example. Editarea is a textbox on the page.

    I'm curious now, does anyone know how to make it select a whole form, or a table, or the body of the page?

  12. #12
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    Originally posted by kin
    You may already know this, but the .execCommand("SelectAll") and .execCommand("Copy") do work inside a function to perform the said actions, but I don't know if you can get them to select the whole page. I can get them to select a whole textbox. Ex.

    function ClipBoard2()
    {
    Copied = editarea.createTextRange();
    Copied.execCommand("SelectAll");
    Copied.execCommand("Copy");
    }

    Of course, for this command, Copied.execCommand("SelectAll"); isn't necessary, but just there as example. Editarea is a textbox on the page.

    I'm curious now, does anyone know how to make it select a whole form, or a table, or the body of the page?
    I wonder if it would work if "editarea" was a named DIV?...and whatever was inside the DIV gets copied?

  13. #13
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    This works...can it be narrowed down to only grab one part of a page?

    <html>
    <head>
    <title>Copy to clipboard</title>
    <script>
    function copytoclipboard()
    {
    this.document.execCommand("SelectAll", true);
    this.document.execCommand("Copy", true);
    this.document.execCommand("UnSelect", true);
    }
    </script>
    </head>
    <body>
    <table>
    <tr>
    <td>blah1</td>
    <td>blah2 blah2</td>
    <td>blah3 blah3 blah3</td>
    </tr>
    </table>
    <a href="#" onclick="copytoclipboard();">Click Me</a>

    </table>
    </body>
    </html>

  14. #14
    Join Date
    Mar 2005
    Posts
    121
    That I don't know. I can't figure out how. If you give your table a name, is there a way to reference it on the this.document line?

  15. #15
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    Originally posted by kin
    That I don't know. I can't figure out how. If you give your table a name, is there a way to reference it on the this.document line?
    You'd think there would be a way.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles