QR Code Generator
QR generator — paste link & download
Tip: paste then press Generate. If you paste a domain without protocol we add
(function(){
const input = document.getElementById('url');
const btn = document.getElementById('gen');
const canvas = document.getElementById('qr');
const download = document.getElementById('download');
// Validate & normalize URL: if no scheme, add https://
function normalizeUrl(raw){
if(!raw) return '';
let s = raw.trim();
// quick check for spaces (reject)
if(/\s/.test(s)) return '';
try {
// if it has scheme, let URL constructor validate
new URL(s);
return s;
} catch(e) {
// try adding https://
try {
const withProto = 'https://' + s;
new URL(withProto);
return withProto;
} catch(e2){
return '';
}
}
}
// Generate QR onto canvas and prepare download link
async function generateQR(){
const raw = input.value;
const url = normalizeUrl(raw);
if(!url){
alert('Please paste a valid link (no spaces).');
return;
}
// Options: error correction H (best), scale controls pixel size.
const opts = {
errorCorrectionLevel: 'H',
type: 'image/png',
quality: 0.92,
margin: 2, // quiet zone
scale: 8 // 8 -> ~256px for small data; increase for higher res
};
try {
// Use library to draw to canvas
await QRCode.toCanvas(canvas, url, opts);
// Update download link
download.href = canvas.toDataURL('image/png');
download.style.display = 'inline-block';
// Set a helpful filename derived from hostname
try {
const hostname = (new URL(url)).hostname.replace(/^www\./,'');
download.download = `qr-${hostname}.png`;
} catch(e){ download.download = 'qr.png'; }
} catch(err){
console.error(err);
alert('Failed to generate QR code. See console for details.');
}
}
// Events
btn.addEventListener('click', generateQR);
// Generate on paste (user asked paste-friendly)
input.addEventListener('paste', function(e){
// Wait a tick for paste event to populate input
setTimeout(generateQR, 150);
});
// Optional: generate on Enter
input.addEventListener('keydown', function(e){
if(e.key === 'Enter') generateQR();
});
// Provide a small default example
input.value = '';
// If you want an initial demo, uncomment:
// input.value = 'https://example.com'; generateQR();
})();
https:// automatically.