Generate QR code using jquery

Source: Internet
Author: User

Generate QR code using jquery

Go to the topic:

Introduce jquery js and jquery-qrcode.js. I will upload the code demo to the resource library later. The code is simple:

It is easier to generate a QR code for non-Chinese characters.

 <script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/jquery.qrcode.min.js"></script><script>$(function() {$("#qrcode").qrcode("welcome to China");});</script>
If it is a Chinese character or contain Chinese characters, then you need to do some encoding conversion, the UTF-16 to the UTF-8

 <Script type = "text/javascript" src = "js/jquery. min. js "> </script> <script type =" text/javascript "src =" js/jquery. qrcode. min. js "> </script> <script type =" text/javascript "src =" js/convertCN. js "> </script> <script >$ (function () {$ (" # qrcode "). qrcode (utf16to8 ("Huang Bin") ;}); </script>

The content of convertCN. js 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 introduction of the third js is customized to solve the problem that Chinese characters cannot be identified. Compared with Java servlet, jquery is faster, more convenient, and relies on fewer plug-ins.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.