Search code examples
jqueryapachewebserver

Cannot copy to clipboard on live server, only on local server


I have a jQuery code that works only on my local USBWebserver V8 test server (PHP 5.4.17), but once the site is uploaded to internet and live (PHP 5.6), it does not work anymore.

My code should just copy URL link into the clipboard. It looks like this:

jQuery('.hardlink').click(function()
{
    var hardlink = jQuery(this).attr("data-hardlink");
    navigator.clipboard.writeText(hardlink).then(function () {
        alert('SOME TEXT HERE')
    }, function () {
        alert('SOME OTHER TEXT HERE')
    });
});

Is there some kind of Apache/PHP settings that needs to be allowed so that the copy to clipboard would work?


Solution

  • So, it turned out that the reason for this "strange" behavior is that the Clipboard API only runs over HTTPS, not HTTP. Info src: https://www.freecodecamp.org/news/copy-text-to-clipboard-javascript/

    Once I added SSL Credentials so that my website runs on HTTPS all works as expected.