search
HomeWeb Front-endJS Tutorialjquery plug-in qrcode generates QR code online_jquery

With the development of mobile Internet, QR codes are now more and more widely used. By scanning them, you can browse websites, add friends, etc. It is really much more convenient than entering them manually.

We made a comprehensive evaluation system in the early stage, considering the gradual implementation of mobile. It is inconvenient for users to enter a long list of IP addresses. With the help of QR codes, users can directly enter the system by picking up their mobile phones and scanning.

Based on this application scenario, I went online to research how to implement website QR codes. In summary, there are two types:

1. Use some QR code generating websites or QR code generators to generate QR code images, and then hang them on the website, such as Code Cloud QR-Code (QR code) online generator

Advantages: Development cost is zero, diversified QR codes can be quickly realized;

Disadvantages: Changing the QR code and maintaining it is a bit troublesome

2. Use java or .net code on the backend to generate QR code images, and then reference the images on the website, such as qrcode, zxing, etc.

Advantages: Highly customizable, fast batch generation

Disadvantages: heavyweight implementation, higher development cost for simple applications

3. Instantly generate a QR code (ˇ?ˇ) on the front-end page through JavaScript or other methods, such as jquery-qrcode

Advantages: lightweight implementation, reducing image IO, saving traffic

Disadvantages: Not suitable for generating complex QR codes

Of course, in actual applications, these three implementation methods are not completely isolated. We can also combine applications according to the actual situation of the project to maximize efficiency and save costs.

I didn’t have much time at night so I chose jquery-qrcode to study.

jquery-qrcode

jquery-qrcode is a jquery plug-in that can generate QR codes on the browser side. It is self-contained, less than 4k with minimal compression, and has no image download requests. After introducing this class library, you can easily add QR codes to web pages with just one line of code.

It is hosted on github: https://github.com/jeromeetienne/jquery-qrcode

jquery-qrcode mainly contains two files:

1. qrcode.js: QR code algorithm implementation class

2. jquery.qrcode.js: Use jquery to encapsulate qrcode.js, and implement canvas and table rendering to generate QR codes according to user parameters

After compression, there is only one file jquery.qrcode.min.js.

Code implementation

There are actually very detailed instructions and examples on github, so I won’t go into details here.

However, for the convenience of future use, I will reorganize the code based on everyone’s experience on the Internet.

jquery-qrcode.html code is as follows:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>通过jquery-qrcode生成二维码</title>
</head>
<body>
<!-- 引入百度CDN公共库的压缩版jQuery -->
<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
<!--引入压缩版jquery.qrcode.js -->
<script src="jquery.qrcode.min.js"></script>
<!--未压缩则需要引入两个文件jquery.qrcode.js和qrcode.js -->
<!--jquery.qrcode.js:jquery封装渲染类库 -->
<!--<script src="jquery.qrcode.js"></script>-->
<!--qrcode.js:二维码核心计算类库 -->
<!--<script src="qrcode.js"></script>-->
<script src="jquery.qrcode.min.js"></script>
<!--解决中文乱码问题,引入utf16t8.js -->
<script src="utf16to8.js"></script>

<div id="qrcodeCanvas"></div>

<div id="qrcodeTable"></div>
<script>
 //最简用法,render默认是canvas
 $('#qrcodeCanvas').qrcode("http://www.jb51.net/");
 //完整用法,有默认值的均可省略
 $('#qrcodeTable').qrcode({
  text: utf16to8("脚本之家:http://www.jb51.net/"),//二维码包含的内容,默认只支持英文内容,中文会乱码,通过utf16to8转码可支持中文
  render: "table",//渲染方式可选择canvas或table,默认是canvas,canvas方式还支持右键图片下载
  width: 256,//宽度,默认是256
  height: 256,//高度,默认是256,建议宽度和高度保持一致,否则不容易被识别
  typeNumber: -1,//计算模式,默认是-1
  //correctLevel: QRErrorCorrectLevel.H,//纠错等级,默认是QRErrorCorrectLevel.H,但是加上correctLevel这一行后无法渲染出二维码
  background: "#ffffff",//背景颜色,默认是白色
  foreground: "#000000"//前景颜色,默认是黑色
 });
</script>
<body>
</html>

Based on the official sample test, we will find that the recognized Chinese QR code will be garbled.

According to the explanation from netizens with good intentions:

This is related to the js mechanism. The jquery-qrcode library uses charCodeAt() to perform encoding conversion,

This method will obtain its Unicode encoding by default. Generally, decoders use UTF-8, ISO-8859-1, etc.,

There is no problem in English. If it is Chinese, generally Unicode is implemented in UTF-16, with a length of 2 digits, while UTF-8 encoding is 3 digits, so the encoding and decoding of the QR code will not match.

The solution is of course to convert the string into UTF-8 before encoding the QR code

So we can solve this problem with the help of utf16to8.js. The specific code is as follows:

function utf16to8(str) {
 var out, i, len, c;
 out = "";
 len = str.length;
 for (i = 0; i < len; i++) {
  c = str.charCodeAt(i);
  if ((c >= 0x0001) && (c <= 0x007F)) {
   out += str.charAt(i);
  } else if (c > 0x07FF) {
   out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
   out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
   out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  } else {
   out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
   out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  }
 }
 return out;
}

The above is all the content shared with you today. I hope it will be helpful to everyone learning jQuery.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software