Copy to clipboard doesn't work on Chrome in VueJS


Vinavi Anutara

In my VueJS app, I have a component that copies the base URL to the clipboard by clicking a link

<a @click="copyURL" ref="mylink">
            <img class="social_icon" src="/images/game/copy-fr.png" alt="Copy icon"
          /></a>
          <input type="text" class="copyurl_txt" v-model="base" ref="text" />
          <div v-if="flash" v-text="flash"></div>

I have the following method in my script,

copyURL() {
      this.$refs.text.select();
      document.execCommand("copy");
      this.flash = "lien copié dans le presse-papiers";
    },

This works fine on my Firefox browser, but on my Chrome this doesn't copy the link to the clipboard...

Volka Dimitrev
<a @click="copyURL" ref="mylink">
            <img class="social_icon" src="/images/game/copy-fr.png" alt="Copy icon"
          /></a>

Your method should be as follows,

copyURL() {
            const el = document.createElement('textarea');  
            el.value = window.location.origin;                                 
            el.setAttribute('readonly', '');                
            el.style.position = 'absolute';                     
            el.style.left = '-9999px';                      
            document.body.appendChild(el);                  
            const selected =  document.getSelection().rangeCount > 0  ? document.getSelection().getRangeAt(0) : false;                                    
            el.select();                                    
            document.execCommand('copy');                   
            document.body.removeChild(el);                  
            if (selected) {                                 
              document.getSelection().removeAllRanges();    
              document.getSelection().addRange(selected);   
            }
            this.flash = "lien copié dans le presse-papiers";
        },

If you want to use a different value instead of the base url then simply change

el.value = window.location.origin;

arrive

el.value = this.link_url; 

or

el.value = "www.yourlink.com";

Related


JavaScript copy to clipboard doesn't work

User7570296: I have a function in my script that gives me an error. The purpose of this function is to copy text from a static panel (not a textbox or input) via the onClick event. Uncaught TypeError: copyText.select is not a function What I want is to enable

Copy to clipboard doesn't work javascript

Christian Poland var $temp = $("<input>"); $("body").append($temp); $temp.val('123').select(); document.execCommand("copy"); $temp.remove(); What's wrong with this code? should be correct Doesn't work at all for me. I've read a lot of topics here and this see

JavaScript copy to clipboard doesn't work

User7570296: I have a function in my script that gives me an error. The purpose of this function is to copy text from a static panel (not a textbox or input) via the onClick event. Uncaught TypeError: copyText.select is not a function What I want is to enable

HTML copy to clipboard button doesn't work

Oscar I'm trying to copy a link ( www.google.com ) to the clipboard using a button, but it doesn't work. My code is: <script type='text/javascript'> function myFunction() { /* Get the text field */ var copyText = document.getElementById("www.go

clipboard doesn't work

Daniellerondiyev I use Arch Linux (4.10.13-1-ARCH). Xfce environment. A few days ago I thought the clipboard was not working properly on Google Chrome, Onlyoffice, Okular, Gimp. It still works in a similar way: Termianl <-> Mouse Pad, Terminal <-> Terminal, Mo

clipboard doesn't work

Daniellerondiyev I use Arch Linux (4.10.13-1-ARCH). Xfce environment. A few days ago I thought the clipboard was not working properly on Google Chrome, Onlyoffice, Okular, Gimp. It still works in a similar way: Termianl <-> Mouse Pad, Terminal <-> Terminal, Mo

Copy to global clipboard doesn't work with Java in Ubuntu

Carousel: The following code from a standalone app works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Unsupported

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy to global clipboard doesn't work with Java in Ubuntu

Carousel: The following code from a standalone app works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Unsupported

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

C# copy to clipboard doesn't work at runtime

Samar I am using asp.net and c# on a web application in code behind. I just want to copy the text in the textbox to the clipboard, my code is as follows: The problem is, this works fine in debug mode, but when deploying my website, in run mode, CopyToClipboard

JavaScript copy textarea value to clipboard doesn't work

user 678976 I'm trying to copy the content of a textarea to the clipboard using javascript, but it doesn't work. When I check the clipboard it is empty. My Javascript is: function copyStudentEmails() { /* Get the text field */ var copyStudentEmail = docume

Copy to global clipboard doesn't work with Java in Ubuntu

Carousel: The following code from a standalone app works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Unsupported

ng-clip copy to clipboard doesn't work

Bharat I am using some tutorials to implement ng-clip. I'm following the tutorial and it's not working. I include Zeroclipboard.min.js, angular.js, ngClip.js HTML looks like.. <div ng-app="clip"> <button clip-copy="getTextToCopy()">Copy</button>

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

Copy from table td to clipboard doesn't work

Besenyi Can you take a look at this demo and let me know why I can't copy the previous text tdafter clicking it .btn-copy? $('.btn-copy').on('click', function(){ $(this).closest('td').prev('td').select(); try { var successful = document.execCommand(

C# copy to clipboard doesn't work at runtime

Samar I am using asp.net and c# on a web application in code behind. I just want to copy the text in the textbox to the clipboard, my code is as follows: The problem is, this works fine in debug mode, but when deploying my website, in run mode, CopyToClipboard

"+y doesn't copy to clipboard

Vico I am trying to copy a piece of text to the system clipboard. First, I make an intuitive choice: select ", and +then y. Then I get a message 7 lines yanked. When I press p, the data is pasted vim; however, when I try to paste the data into any other progra

"+y doesn't copy to clipboard

Vico I am trying to copy a piece of text to the system clipboard. First, I make an intuitive choice: select ", and +then y. Then I get a message 7 lines yanked. When I press p, the data is pasted vim; however, when I try to paste the data into any other progra