What is QRCode.js?
QRCode.js is a JavaScript library for generating QR codes. It works mainly by obtaining DOM elements and then drawing them via the HTML5 Canvas, without depending on any libraries.
Basic Usage
<div id="qrcode"></div>
<script type="text/javascript">
new QRCode(document.getElementById("qrcode"), "https://www.example.com"); // 设置要生成二维码的链接
</script>
Or use some optional parameter settings:
var qrcode = new QRCode("test", {
text: "https://www.example.com",
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#FFFFFF",
correctLevel : QRCode.CorrectLevel.H
});
We can also use the following methods:
qrcode.clear(); // 清除代码
qrcode.makeCode("https://www.jyshare.com"); // 生成另外一个二维码
Browser Support
Browsers that support this library include: IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile, etc.
Example Code
HTML Code
<input id="text" type="text" value="https://www.example.com" /><br /> <div id="qrcode"></div>
CSS Code
#qrcode {
width:160px;
height:160px;
margin-top:15px;
}
JavaScript Code
var qrcode = new QRCode("qrcode");
function makeCode () {
var elText = document.getElementById("text");
if (!elText.value) {
alert("Input a text");
elText.focus();
return;
}
qrcode.makeCode(elText.value);
}
makeCode();
$("#text").
on("blur", function () {
makeCode();
}).
on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode();
}
});
Resource Download
Qrcode library: https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js