I am fairly new in working with ASP.NET and Javascript. Recently I came across this problem where I had to copy the URL on a javascript button action and paste on a new tab to visit the website. It does work locally but not on the live server. I found out that it was due to not adding 'https'. Is there any way where it can work without using 'https' like 'http' only?
function CopyTextFunction() {
const params = new URLSearchParams(window.location.search);
params.get('ID')
var copyText = "=" + params.get('ID');
console.log(copyText);
navigator.clipboard
.writeText(copyText)
.then(() => {
alert("successfully copied");
})
.catch(() => {
alert("something went wrong");
});
}
I am fairly new in working with ASP.NET and Javascript. Recently I came across this problem where I had to copy the URL on a javascript button action and paste on a new tab to visit the website. It does work locally but not on the live server. I found out that it was due to not adding 'https'. Is there any way where it can work without using 'https' like 'http' only?
function CopyTextFunction() {
const params = new URLSearchParams(window.location.search);
params.get('ID')
var copyText = "https://randomshop/OnlineShop/ShopProducts?ID=" + params.get('ID');
console.log(copyText);
navigator.clipboard
.writeText(copyText)
.then(() => {
alert("successfully copied");
})
.catch(() => {
alert("something went wrong");
});
}
Share
Improve this question
edited May 14, 2022 at 11:40
Alicia Sykes
7,1078 gold badges38 silver badges70 bronze badges
asked May 14, 2022 at 5:58
HU EWHU EW
931 gold badge1 silver badge5 bronze badges
1
-
I shared a version that works with plain-HTTP and copies as
text/html
MIME type (with styles, etc., so good for pasting into apps expecting rich text like Excel, etc.) over at stackoverflow./a/77489472/500207 which with thedocument.execCommand
but using theRange
API. – Ahmed Fasih Commented Nov 15, 2023 at 17:01
1 Answer
Reset to default 14As you correctly stated this is due to not using HTTPS. According to MDN Clipboard
docs: "This feature is available only in secure contexts".
The best option would be to just use HTTPS.
But since you asked for a workaround, here's a (hacky) working sample. It uses the Document.exec
mand, which in the future will be deprecated in favor of ClipboardAPI
.
function unsecuredCopyToClipboard(text) {
const textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
} catch (err) {
console.error('Unable to copy to clipboard', err);
}
document.body.removeChild(textArea);
}
You could then use navigator.clipboard == undefined
to use the fallback method, otherwise use the normal navigator.clipboard.writeText(...)
function where supported.
E.g, from your above code:
const copyText = `https://randomshop/OnlineShop/ShopProducts?ID=${params.get('ID')}`;
if (navigator.clipboard) { // If normal copy method available, use it
navigator.clipboard.writeText(copyText);
} else { // Otherwise fallback to the above function
unsecuredCopyToClipboard(copyText);
}