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:


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?

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:

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

I guess we’re searching for solutions too complex :slight_smile:

Someone shared me this:

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

Originally posted by someone4
I guess we’re searching for solutions too complex :slight_smile:
Sheesh! You’re not kidding! :slight_smile:
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 :slight_smile:

Andy