Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy clipboard function working locally but not working server

I am using the below script: It is working locally in visual studio. but in server it throws

"Cannot read property 'write text' of undefined"

 function GetCopyText(thislink) {
    var Content = thislink.parentNode.parentNode.parentNode.parentElement.parentElement.parentElement.parentElement.children[2].children[0].children[0].innerText;

    navigator.clipboard.writeText(Content).then(function () {
        // alert('Async: Copying to clipboard was successful!');
    }, function (err) {
        console.error('Async: Could not copy text: ', err);
    });
}
like image 545
Daniel Stephen Avatar asked Aug 28 '18 09:08

Daniel Stephen


2 Answers

Make the protocol https and the browser should run it.

like image 60
Wade T. Avatar answered Sep 20 '22 17:09

Wade T.


Clipboard feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

Thanks Wade

Suggestions: its better to check navigator.clipboard value and proceed only if exists. This provides good user experience and In large organizations, since many developers will be running UI in local mode for testing, It saves their debugging time and then figure out navigator.clipboard is not available in local mode without https.

like image 35
Vinayak V Naik Avatar answered Sep 21 '22 17:09

Vinayak V Naik