Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created July 2, 2023 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/dbd61143b014c87d8e920db9c1896a26 to your computer and use it in GitHub Desktop.
Save code-boxx/dbd61143b014c87d8e920db9c1896a26 to your computer and use it in GitHub Desktop.
Javascript Generate QR Code

JAVASCRIPT GENERATE QR CODE

https://code-boxx.com/generate-qr-code-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>QR Code Generate</title>
<!-- (A) LOAD QRCODEJS LIBRARY -->
<!-- https://cdnjs.com/libraries/qrcodejs -->
<!-- https://github.com/davidshimjs/qrcodejs -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
</head>
<body>
<!-- (B) GENERATE QR CODE HERE -->
<div id="qrcode"></div>
<!-- (C) CREATE QR CODE ON PAGE LOAD -->
<script>
window.addEventListener("load", () => {
var qrc = new QRCode(document.getElementById("qrcode"), "https://code-boxx.com/");
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>QR Code Generate</title>
<!-- (A) LOAD QRCODEJS LIBRARY -->
<!-- https://cdnjs.com/libraries/qrcodejs -->
<!-- https://github.com/davidshimjs/qrcodejs -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
</head>
<body>
<!-- (B) GENERATE QR CODE HERE -->
<div id="qrcode"></div>
<!-- (C) CREATE QR CODE ON PAGE LOAD -->
<script>
window.addEventListener("load", () => {
var qrc = new QRCode(document.getElementById("qrcode"), {
text: "https://code-boxx.com/",
width: 100,
height: 100,
colorDark: "#ff0000",
colorLight: "#ffffff",
// QRCode.CorrectLevel.L | QRCode.CorrectLevel.M | QRCode.CorrectLevel.H
correctLevel : QRCode.CorrectLevel.H
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>QR Code Generate</title>
<!-- (A) LOAD QRCODEJS LIBRARY -->
<!-- https://cdnjs.com/libraries/qrcodejs -->
<!-- https://github.com/davidshimjs/qrcodejs -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
</head>
<body>
<!-- (B) GENERATE QR CODE HERE -->
Website <div id="qrA"></div>
Tel <div id="qrB"></div>
SMS <div id="qrC"></div>
VCARD <div id="qrD"></div>
<!-- (C) CREATE QR CODE ON PAGE LOAD -->
<!-- https://github.com/zxing/zxing/wiki/Barcode-Contents -->
<script>
window.addEventListener("load", () => {
// (C1) WEBSITE
new QRCode(document.getElementById("qrA"), "https://code-boxx.com/");
// (C2) TEL
new QRCode(document.getElementById("qrB"), "tel:+12345678");
// (C3) SMS
new QRCode(document.getElementById("qrC"), "sms:+12345678");
// (C4) VCARD
let card = "BEGIN:VCARD\r\n";
card += "VERSION:3.0\r\n";
card += "FN:John Doe\r\n";
card += "TEL;TYPE=home:tel:+1-123-456-789";
card += "EMAIL;TYPE=internet:john@doe.com";
new QRCode(document.getElementById("qrD"), card);
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>QR Code Generate</title>
<!-- (A) LOAD QRCODEJS LIBRARY -->
<!-- https://cdnjs.com/libraries/qrcodejs -->
<!-- https://github.com/davidshimjs/qrcodejs -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
</head>
<body>
<!-- (B) QR CODE HERE -->
<div id="printable">
<h1>FOO BAR!</h1>
<p>This can be a Vcard, event, special offer, or something that you want users to print.</p>
<div id="qrcode"></div>
</div>
<button id="qrprint">PRINT</button>
<!-- (C) CREATE QR CODE ON PAGE LOAD -->
<script>
window.addEventListener("load", () => {
// (C1) CREATE QR
var qrc = new QRCode(document.getElementById("qrcode"), {
text: "https://code-boxx.com/",
width: 100,
height: 100,
colorDark: "#bf2a2a"
});
// (C2) PRINT
document.getElementById("qrprint").onclick = () => {
var printwin = window.open("");
printwin.document.write(document.getElementById("printable").innerHTML);
printwin.stop();
let qr = printwin.document.querySelector("#qrcode img");
qr.addEventListener("load", () => {
printwin.print();
printwin.close();
});
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment