#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7

    OnClick copy to clipboard?


    Howdy. I am wrinting this site for a tech company. One page has a list of programs followed by their download address. They wish for me to not make this a link, simply because their intent is to be able to copy this string and paste it to a client.

    I currently have A table built with 2 columns. First being name, second being address. Is there something I can do so when the PHP loop finishes building the table, that a tech can just click the address text and have it copied to clipboard?

    Thanks for any help.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    JavaScript's ability to interact with the clipboard is extremely limited. If you need to read or write to or from the clipboard, you will likely need to use Flash or a browser plugin.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    Try using Zero Clipboard.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Apparently these techs r more familiar with Ctrl + C than they are with Right Click + Copy, so I made it onClick select its text, for their Ctrl + C. Thanks all for input.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0
    There was a project I was working on where we were looking for a similar solution.

    It has already been mentioned that access to the clipboard is very limited and for good security reasons.

    There are ways round it but you would often need different solutions per browser.

    I would like to know if you came up with a solid cross browser solution

    Thanks,

    Leon.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    I didn't end up working something that posted to the keyboard. The techs were used to hitting Ctrl + C, so I ran the 2 items:

    Javascript:
    Code:
    <script>
    function selectText(element) {
        var doc = document;
        var text = doc.getElementById(element);    
    
        if (doc.body.createTextRange) { // ms
            var range = doc.body.createTextRange();
            range.moveToElementText(text);
            range.select();
        } else if (window.getSelection) { // moz, opera, webkit
            var selection = window.getSelection();            
            var range = doc.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range);
        }
    }
    
    </script>
    PHP:
    PHP Code:
    <TD id="link">
      <a href="#" id="<?php echo $i?>" onClick="selectText(<?php echo $i?>)"><?php echo $col1?></a>
    </TD>

IMN logo majestic logo threadwatch logo seochat tools logo