copy url to clipboard with a click

Discussion in 'JavaScript' started by namooth, Apr 11, 2008.

  1. #1
    Hey all,

    I'm trying to implant something in my blog and i hope you can help me.
    I want my readers to have the option to click a button \ link like the trackback one and that the post link - URL will be automatically copied to their clipboard (like you see on imageshack, photobucket, TinyPic etc')
    I've managed to find a code that does just that but its written in javascript. Due to today security in browsers you need to use a swf file in order for it to work.


    Here's the code:

    function copy(inElement) {
      if (inElement.createTextRange) {
        var range = inElement.createTextRange();
        if (range && BodyLoaded==1)
          range.execCommand('Copy');
      } else {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
          var divholder = document.createElement('div');
          divholder.id = flashcopier;
          document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
      }
    }
    
    Code (markup):
    and the swf file: hxxp://www.jeffothy.com/weblog/uploads/clipboard.php

    how can i use the script above to copy my current url?

    Thanks
     
    namooth, Apr 11, 2008 IP
  2. namooth

    namooth Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    i'll make it easier to understand what im after.

    i want to change this html that instead of the text element the script "copy" will copy to the clipboard the current URL. I've tried to call it with location.href but with no luck.

    
    <form name="formtocopy" action="">
    <textarea name="texttocopy">
    A whole bunch of text here that will be copied.
    </textarea>
    <br>
    <a href="javascript:copy(document.formtocopy.texttocopy);">copy</a>
    </form>
    
    Code (markup):
     
    namooth, Apr 11, 2008 IP
  3. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this, good luck :)
     
    Morishani, Apr 11, 2008 IP
  4. namooth

    namooth Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    im still getting "undefined" into my clipboard...



    תודה, :)
     
    namooth, Apr 11, 2008 IP