SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Location
    belgium
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copy URL to clipboard script

    Hi all,

    I'm fairly new to javascript, and I'm looking for a buttonscript which basicly copies the URL of the page a visitor is on to his computer clipboard. I guess it will be a IE only feature, but that doesn't matter.

    I have found some code to copy texts to a clipboard, but I'm wondering if it would be possible to get the URL and copy that with a single click.

    The code I found so far is this:

    Code:
    function copyTo(obj)
    {
     if (obj.type == "text") var oControlRange = obj.createTextRange()
      else{var oControlRange = document.body.createTextRange();
           oControlRange.moveToElementText(obj);}
      oControlRange.execCommand("copy");
    }
    In worst case scenario, the current url could be generated by the server (php).

    Any ideas how to proceed from here?

  2. #2
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been looking for a way of writing to the clipboard using Javascript for a while, but I can't actually get yours to work.

    You could just put one of the following inside your function to get the current URL:
    Code:
    var currentURL=location.href;
    var currentURLascii=unescape(location.href);
    then use the remainder of the function to write the value of currentURL to the clipboard.

    I'd be interested in seeing the completed version.


    Andy
    From the English nation to a US location.

  3. #3
    SitePoint Member
    Join Date
    Feb 2003
    Location
    belgium
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess we're searching for solutions too complex

    Someone shared me this:

    Code:
     function copyURLtoClipboard()
                {
                window.clipboardData.setData("Text",location.href);
                }

  4. #4
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by someone4
    I guess we're searching for solutions too complex
    Sheesh! You're not kidding!
    Thanks a lot for posting that. It's something that I can really use for some intranet stuff that I'm working on.

    If you need that url of yours manipulating any before pushing it to the clipboard, then check out this script here.


    Oh, and welcome to SPF

    Andy
    From the English nation to a US location.


Bookmarks

Posting Permissions

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