var copy = function(code, context) { if (code.length) { // alert(code); var input = document.createElement('textarea'); input.innerHTML = code; input.id = 'temporary'; /* input.style.background = 'transparent'; input.style.border = '0'; input.style.padding = '0'; input.style.height = '10px'; input.style.width = '10px'; */ input.style.height = '0'; input.style.position = 'fixed'; input.style.top = '0'; //input.style.left = '0'; input.style.fontSize = '16px'; document.body.appendChild(input); if (navigator.userAgent.match(/ipad|iphone/i)) { var editable = input.contentEditable; var readOnly = input.readOnly; input.contentEditable = true; input.readOnly = false; var range = document.createRange(); range.selectNodeContents(input); var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); input.setSelectionRange(0, 999999); input.contentEditable = editable; input.readOnly = readOnly; } else { input.select(); } try { exec = document.execCommand('copy'); document.getElementById('temporary').remove(); if (window.alert) { console.log('1. Den är uppe'); } alert(exec ? (context ? '' + context + '' : 'Kod') + ' har kopierats' : 'Du verkar inte kunna kopiera 😩'); // 🖱 if (window.alert) { console.log('2. Fortfarande uppe'); } } catch (err) { console.error('Oops, unable to copy'); alert('1. Stöd för att kopiera kod saknas i din webbläsare'); } } else { alert('Det finns ingen kod att kopiera 🤔'); } } function copyToClipboard(text) { var textArea = document.createElement("textarea"); textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.position = 'fixed'; textArea.style.top = 0; textArea.style.left = 0; textArea.style.padding = 0; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.select(); try { var successful = document.execCommand('copy'); // Går det att lösa? //alert(document.execCommand('paste')); alert('Koden har nu kopierats! 📋'); var msg = successful ? 'successful' : 'unsuccessful'; console.log('Copying text command was ' + msg); } catch (err) { console.error('Oops, unable to copy'); alert('2. Stöd för att kopiera kod saknas i din webbläsare'); } document.body.removeChild(textArea); }