DEV Community

Cover image for How to make a QR Code generator using JavaScript?

How to make a QR Code generator using JavaScript?

Murtuzaali Surti on December 23, 2021

While you can generate QR codes for URLs in browsers such as Chrome, it's always interesting to learn how you can make your own version of a simple...
Collapse
 
activenode profile image
David Lorenz

Thank you for the article. It shows how to generate a QR Code which can be helpful. However I suggest you change the title toCreating a QR Code with JavaScript because the term Vanilla JS refers to using pure JavaScript. When I read the title I clicked the article because I thought you were actually showing it with Vanilla JS but instead you are using a library which is not Vanilla JS. Try to imagine this: I tell someone "How to get a DOM Element with VanillaJS" and then I show how to load jQuery to then query with jQuery the DOM Element.

Again: The article in itself is helpful but the title very misleading.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

A number of issues here....

You're using a submit button without a form. Adding a form would actually be better too, as you could generate the QR code by also pressing return to submit the form. This is standard, expected behaviour on a form.

You're repeatedly calling document.querySelector to get the same things. Would be better just to call it once.

If you have an element with an id - you don't need to use document.querySelector('#id') at all - the element will already be available as window.id, or just id.

Putting all of this together, you can simplify your code considerably.

Also, this isn't really a QR code generator at all - it's an interface to call a 3rd party function that generates a QR code. You should try writing such a generator yourself - it's quite interesting, and you find out how QR codes work.

Collapse
 
activenode profile image
David Lorenz

I agree to everything but the ID approach. Using global definitions is not recommended as there is technically no guarantee it's gonna be the DOM Element (stackoverflow.com/a/18713391/1236627).

Collapse
 
jonrandy profile image
Jon Randy 🎖️

In simple code like your example, you can guarantee it

Thread Thread
 
activenode profile image
David Lorenz • Edited

Context-free, yes. But that wasn't my point. The point is that it is generally not recommended besides for e.g. CodePens or Debugging sessions so I recommend to be careful with that advice. (Or in other words: Even in a large-scale enterprise application I could state "but here it is working" - which then only holds true as long as the overall context (switching libraries, new employees adding new code, etc) does not change. Which is not a good idea.)

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Yup - agree. It's appropriate to use in the right context. It should never be avoided just because people "don't understand it" or "aren't used to doing it that way" - way too much of that goes on these days

Thread Thread
 
activenode profile image
David Lorenz • Edited

The problem is that this is viewed from a "small context perspective". In production, as stated, it shouldn't be recommended to be used.

It goes the same principle as always e.g. Security through Obfuscation is definitely working in the right context. But it's recommended to be avoided at all costs in programming because it opens up an option to be breached.

And the similiar goes for window[[id] . If being used in non-prod envs such as showing something or Sandboxes then it can be very helpful (this is where I actually do use it).

But for prod it should be avoided as a guideline. Generally in JS we try to modularize more to enable teams to scale and be free from dependencies from other teams.

Now imagine one team has the idea of using window[id] for their component that they are building. And say both teams are using e.g. a submitButton which is very likely. Now because they allowed themselves to do so they would be allowed to add id="submitButton" . So not only now the ID is double-placed but also the window[id] is ambigious. So the teams are suddenly very dependent on each other, communication effort increases, etc.

On top of that comes another problem: You are polluting the global context. Libraries are polluting the global context too - because they have to. E.g. analytics libraries might expose const analytics = whatever in the window scope. That happens in enterprise applications extremely often because the 3d-party tracking tool needs access to it. The const analytics now takes precedence over any DOM ID definition. So now the approach of using window[domId] wouldn't work anymore.

In any prod-bound or team-bound contexts where more than 1 person works or anything is supposed to scale in the future this should definitely not be used.

Thread Thread
 
lionelrowe profile image
lionel-rowe • Edited

From the HTML spec:

window[name]: As a general rule, relying on this will lead to brittle code. Which IDs end up mapping to this API can vary over time, as new features are added to the web platform, for example. Instead of this, use document.getElementById() or document.querySelector().

The only reason this feature hasn't been removed is that (as with many other bizarre features of the early Web) it can't be changed without breaking legacy code. It's the same reason you can write <body bgcolor="butts"> and get a handsome brick-red background for your page.

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti

Yup I agree that I used a JS library but this is actually a beginner friendly tutorial to make a QR code generator web app on your own.

Collapse
 
kritikgarg profile image
kritikgarg

🙌 Thanks for sharing this amazing article, Murtuzaali! 🤓

I found the step-by-step instructions on how to create a QR code generator using JavaScript to be super helpful. 🤩

I've been wanting to learn more about QR codes and how they can be used in Salesforce, so your article was timely. 🤑

I also recently read the article on How to Use QR Codes in Salesforce and found it to be a great complement to your article. 🤞

Both pieces of content provided valuable insights on the potential uses and benefits of QR codes in Salesforce. 💪

Keep up the great work!

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti

Thank You!

Collapse
 
lexlohr profile image
Alex Lohr

There's a Barcode Detection API in modern browsers. You'd only need to polyfill it if it is not supported.

Collapse
 
activenode profile image
David Lorenz

Which is helping for Detection, not for creation. But thanks for the hint 👍

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti • Edited

It's not available for all browsers. Thanks for suggesting, I didn't knew about this earlier!

Collapse
 
thenickest profile image
TheNickest

Interesting article but the title is misleading. Neither vanilla flavour nor generating anything

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti

This is an article which appeals to beginners!

Collapse
 
thenickest profile image
TheNickest

Does not mean that the title should be misleading!

Thread Thread
 
murtuzaalisurti profile image
Murtuzaali Surti

changed

Collapse
 
techtitans0101 profile image
TechTitans0101 • Edited

Open source is essential for application developers. It is unfortunate that Open Base is shutting down. If you are looking for an alternative, check out kandi from Open Weaver. kandi helps developers build applications faster through open source reuse. Through its natural language interface, kandi recommends code snippets, packages, libraries and solutions from 650 Million open source assets. kandi also promotes responsible reuse with proper license attribution across all assets, while guiding the developer on paring code snippets with libraries to build their applications faster.

Try kandi at kandi.openweaver.com. Thank you for your support to the open source community and happy coding!

Collapse
 
anubhavshakya profile image
Anubhav Shakya

Hey guys,
Is there any library for react-native ??? I need to design stylish qr code.

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti
Collapse
 
techtitans0101 profile image
TechTitans0101 • Edited

Open source is essential for application developers. It is unfortunate that Open Base is shutting down. If you are looking for an alternative, check out kandi from Open Weaver. kandi helps developers build applications faster through open source reuse. Through its natural language interface, kandi recommends code snippets, packages, libraries and solutions from 650 Million open source assets. kandi also promotes responsible reuse with proper license attribution across all assets, while guiding the developer on paring code snippets with libraries to build their applications faster.

Try kandi at kandi.openweaver.com. Thank you for your support to the open source community and happy coding!

Collapse
 
anubhavshakya profile image
Anubhav Shakya

Thanks, I will explore it.

Collapse
 
umairshah143 profile image
Umair Shah

Build Full QR Code Generator Website using JavaScript
QR Code Generator of Link, Text, Phone, Vcard, Message, App Links
Copy QR Code Image to Clipboard
Download QR Code Image
Change QR Code Size and Color
demo tutorial and source code: bit.ly/3OZcxBH

Collapse
 
blaze_droid profile image
Vikas Dongre

You can't call it vanilla, because you are using an library. I think pure vanilla javascript qr code is not even possible.

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti

changed on demand

Collapse
 
mzaini30 profile image
Zen

Why setTimeout? Why not async/await?

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti

Because we don't return any promises which can be resolved!

Collapse
 
murtuzaalisurti profile image
Murtuzaali Surti