Add QR code to the webpage with jquery.qrcode.js

QR code is a quick response code that has a large storage capacity. It consists of a black and white square box that you can be read using smartphones or dedicated QR reading devices to gain access to additional information.

This is used for advertisement, storing website URLs, information, etc.

In this tutorial, I am using the jquery.qrcode.js plugin to generate a custom QR code and embed it on the web page.

You can adjust its size, background, and foreground color with options.

Add QR code to the webpage with jquery.qrcode.js


Contents

  1. Download and Include
  2. Initialize
  3. Demo 1
  4. Options
  5. Demo 2
  6. Conclusion

1. Download and Include

  • Download the library from GitHub.
  • Include jquery.qrcode.js and qrcode.js script with jQuery library.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery-qrcode-master/src/jquery.qrcode.js"></script>
<script type="text/javascript" src="jquery-qrcode-master/src/qrcode.js"></script>

2. Initialize

  • Create a container element.
<div id='qrcode'></div>
  • CallĀ qrcode() method to initialize the library. In the method pass the text or the URL that you want to show after scanning.
jQuery('#qrcode').qrcode("QR code text");

Completed Code

<div id="qrcode"></div>

<!-- Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery-qrcode-master/src/jquery.qrcode.js"></script>
<script type="text/javascript" src="jquery-qrcode-master/src/qrcode.js"></script>
<script>
 // Initialize
 jQuery('#qrcode').qrcode("QR code text"); 
</script>

3. Demo 1

View Demo


4. Options

Customize default behavior with available options.

Syntax ā€“Ā 

$( selector ).qrcode({
  option-name: value, ...
});

Here, are some options ā€“

  • text ā€“ Specify the content that will accessible after scanĀ e.g. text, link, number, etc.
  • width ā€“ Set the width. The default value is 256.
  • height ā€“ Set the height. The default value is 256.
  • render ā€“ It either take table and canvas value to display QR code on the screen. The default value is canvas.
  • typeNumber ā€“ Automatic calculation to generate QR code. The default value is -1 and takes a value up to 40.
  • background ā€“ Set background color. The default value is #ffffff.
  • foreground ā€“ Set foreground color. The default value is #000000.

Example

jQuery('#qrcode').qrcode({ 
   width: 200,
   height: 200,
Ā  Ā render : "table", 
Ā  Ā text : "https://makitweb.com"
});

5. Demo 2

View Demo


6. Conclusion

It is a lightweight jQuery plugin that allows you to easily embed QR code into your web page. Pass content or link in qrcode() method that you want the user can access after scanning.

If you found this tutorial helpful then don't forget to share.

Leave a Comment