Tôi muốn tạo một trang html, có các chức năng như sau: 1. Tạo QR Code thông tin đăng…
- User
Tôi muốn tạo một trang html, có các chức năng như sau:
- Tạo QR Code thông tin đăng nhập wifi tự động.
- Tạo QR Code từ một link (địa chỉ trang web bất kỳ)
- Tạo QR Code văn bản. Hãy giúp tôi lên phương án thực hiện một cách tối ưu nhất.
Assistant
Để tạo một trang HTML hoàn chỉnh với 3 chức năng bạn yêu cầu, mình sẽ đưa ra phương án tối ưu dựa trên các tiêu chí:
- Không cần backend, hoàn toàn chạy trên client.
- Nhẹ, nhanh, dễ triển khai.
- Responsive, dễ dùng trên cả mobile và desktop.
- Không phụ thuộc vào API bên ngoài, tránh rủi ro phụ thuộc.
✅ Phương án: Dùng thư viện JavaScript
qrcode.js- Thư viện: davidshimjs/qrcodejs
- Chỉ cần 1 file HTML + 1 file JS (hoặc CDN).
- Hỗ trợ tạo QR từ chuỗi bất kỳ (text, link, WiFi config).
📦 Cấu trúc file
index.html ├── qrcode.min.js (hoặc dùng CDN) ├── style.css (tùy chọn)
✅ Cách tạo QR WiFi theo chuẩn
WIFI:T:WPA;S:<SSID>;P:<PASS>;;Đây là chuẩn phổ biến để điện thoại tự động nhận diện và kết nối WiFi.
✅ Code hoàn chỉnh (copy-paste chạy ngay)
<!DOCTYPE html> <html lang="vi"> <head> <meta charset="UTF-8"> <title>Tạo QR Code</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script> <style> body { font-family: Arial, sans-serif; margin: 20px; } .section { margin-bottom: 30px; } label, input, button { display: block; margin: 5px 0; } #qrcode > div { margin: 10px 0; } </style> </head> <body> <h1>Tạo QR Code</h1> <!-- 1. QR WiFi --> <div class="section"> <h2>QR WiFi</h2> <label>Tên WiFi (SSID): <input type="text" id="wifiSSID" placeholder="Tên mạng"></label> <label>Mật khẩu: <input type="password" id="wifiPass" placeholder="Mật khẩu"></label> <label>Loại bảo mật: <select id="wifiType"> <option value="WPA">WPA/WPA2</option> <option value="WEP">WEP</option> <option value="nopass">Không mật khẩu</option> </select> </label> <button onclick="generateWifiQR()">Tạo QR WiFi</button> <div id="qrcode-wifi"></div> </div> <!-- 2. QR Link --> <div class="section"> <h2>QR Link</h2> <label>URL: <input type="url" id="linkURL" placeholder="https://example.com"></label> <button onclick="generateLinkQR()">Tạo QR Link</button> <div id="qrcode-link"></div> </div> <!-- 3. QR Văn bản --> <div class="section"> <h2>QR Văn bản</h2> <label>Nội dung: <textarea id="textContent" rows="3" placeholder="Nhập nội dung..."></textarea></label> <button onclick="generateTextQR()">Tạo QR Văn bản</button> <div id="qrcode-text"></div> </div> <script> function clearQR(id) { const el = document.getElementById(id); el.innerHTML = ''; } function generateWifiQR() { const ssid = document.getElementById('wifiSSID').value.trim(); const pass = document.getElementById('wifiPass').value.trim(); const type = document.getElementById('wifiType').value; if (!ssid) return alert('Vui lòng nhập tên WiFi'); const wifiString = `WIFI:T:${type};S:${ssid};P:${pass};;`; clearQR('qrcode-wifi'); new QRCode(document.getElementById('qrcode-wifi'), wifiString); } function generateLinkQR() { const url = document.getElementById('linkURL').value.trim(); if (!url) return alert('Vui lòng nhập URL'); clearQR('qrcode-link'); new QRCode(document.getElementById('qrcode-link'), url); } function generateTextQR() { const text = document.getElementById('textContent').value.trim(); if (!text) return alert('Vui lòng nhập nội dung'); clearQR('qrcode-text'); new QRCode(document.getElementById('qrcode-text'), text); } </script> </body> </html>
✅ Cách dùng
- Lưu file trên thành
index.html. - Mở bằng trình duyệt.
- Nhập thông tin → bấm nút → QR hiện ra ngay.
✅ Tối ưu thêm (tùy chọn)
- Thêm nút tải QR về dạng PNG (dùng
canvas.toDataURL()). - Thêm dark mode.
- Lưu lịch sử bằng
localStorage.
Nếu bạn muốn mình bổ sung thêm tính năng tải QR hoặc lưu lịch sử, mình có thể viết thêm.