I have an (ASP.Net) application that has the following client-side Javascript, to facilitate copying to clipboard:
var ua = window.navigator.userAgent;
var is_ie = /MSIE|Trident/.test(ua);
if (is_ie) {
var input = document.getElementById("inputcopy"); // select it
input.value = text;
input.select();
document.execCommand("copy");
this.focus();
}
else {
navigator.clipboard.writeText(text).then(() => {
writeLog('Copy successful');
if (showalert == true) alert('Copied to clipboard');
}).catch(() => {
writeLog('Copy failed');
if (showalert == true) alert('Copy to clipboard failed');
});
}
We need to be compatible with all "modern" browsers - Chrome, Firefox and, don't shoot the messenger, IE11. The first two are fine, but the latter ...
As IE doesn't support the navigator.clipboard
I've got the if (is_ie)
in there, which works fine. However, IE doesn't know about the Promise
in the non-IE section, and complains horribly about "Invalid Syntax" at
navigator.clipboard.writeText(text).then(() => {
even though it'll never actually run it.
How can I tell IE to ignore that bit of code, or work around this issue? I've looked at conditionally loading a separate JS file based on browser, but that doesn't look like much fun. Is there a better option?