17

I am trying to copy the current page URL in the text area on button click. Somehow I have tried but is not working. http://www.w3schools.com/code/tryit.asp?filename=FAF25LWITXR5


    function Copy() 
    {
        var Url = document.createElement("textarea");
        Url.innerHTML = window.location.href;
        Copied = Url.createTextRange();
        Copied.execCommand("Copy");
    }
<div>
 <input type="button" value="Copy Url" onclick="Copy();" />
 <br />
 Paste: <textarea rows="1" cols="30"></textarea>
</div>

C. Tewalt
  • 2,271
  • 2
  • 30
  • 49
Nithya
  • 1,029
  • 2
  • 14
  • 27

7 Answers7

17

No need to create new textarea. try to get existing textarea by giving some id ('url').

Here is the working example

function Copy() {
  var Url = document.getElementById("url");
  Url.innerHTML = window.location.href;
  console.log(Url.innerHTML)
  Url.select();
  document.execCommand("copy");
}
<div>
  <input type="button" value="Copy Url" onclick="Copy();" />
  <br /> Paste: <textarea id="url" rows="1" cols="30"></textarea>
</div>
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
12

You should not use execCommand anymore, is deprecated, use the Clipboard API:

let url = document.location.href

navigator.clipboard.writeText(url).then(function() {
    console.log('Copied!');
}, function() {
    console.log('Copy error')
});

More on it: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API

Mr.Web
  • 6,992
  • 8
  • 51
  • 86
5

<html>
    <head>
        <title></title>
    </head>
    <script type="text/javascript">
        function Copy() 
            {
                    //var Url = document.createElement("textarea");
                    urlCopied.innerHTML = window.location.href;
                    //Copied = Url.createTextRange();
                    //Copied.execCommand("Copy");
            }
    </script>
    <body>
        <div>
            <input type="button" value="Copy Url" onclick="Copy();" />
            <br />
          
            Paste: <textarea id="urlCopied" rows="1" cols="30"></textarea>
        </div>
    </body>
</html>
hotcakedev
  • 2,194
  • 1
  • 25
  • 47
Angelo
  • 814
  • 8
  • 21
4

Modified your code little bit and it's working.

<html>
  <head>
  <title></title>
</head>
<script type="text/javascript">
        function Copy() 
        {
            var Url = document.getElementById("paste-box");
            Url.value = window.location.href;
            Url.focus();
            Url.select();  
            document.execCommand("Copy");
        }
</script>
<body>
<div>

    <input type="button" value="Copy Url" onclick="Copy();" />
    <br />

    Paste: <textarea id="paste-box" rows="1" cols="30"></textarea>
</div>
</body>
</html>
4

Another solution, no extra html code is needed:

<script>
    $(document).ready(function () {
        $(document).on("click", "#ShareButton", function (e) {
            $("body").append('<input id="copyURL" type="text" value="" />');
            $("#copyURL").val(window.location.href).select();
            document.execCommand("copy");
            $("#copyURL").remove();            
        });
    });
</script>
ehsan_kabiri_33
  • 341
  • 6
  • 13
  • 1
    went to automatically downvote a jQuery answer but then noticed the question was tagged "jquery". sorry about that... – George Jan 11 '21 at 21:14
2

When the button is clicked select the content of #url then copy it to the clipboard.

<html>
  <body>
    <input type="button" value="Copy Url" id="copy" />
    <br />
    Paste: <textarea rows="1" cols="30" id="url"></textarea>
    <script type="text/javascript">
    document.querySelector("#copy").onclick = function() {
      document.querySelector("#url").select();
      document.execCommand('copy');
    };
    </script>
  </body>
</html>
Johnson Doe
  • 159
  • 4
  • 11
0

 function Copy() 
            {
                    //var Url = document.createElement("textarea");
                    urlCopied.innerHTML = window.location.href;
                    //Copied = Url.createTextRange();
                    //Copied.execCommand("Copy");
            }
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div>
            <input type="button" value="Copy Url" onclick="Copy();" />
            <br />
          
            Paste: <textarea id="urlCopied" rows="1" cols="30"></textarea>
        </div>
    </body>
</html>
Sfili_81
  • 2,377
  • 8
  • 27
  • 36
DENOSTE
  • 1
  • 1