Trang chủJSBài viết
JS

Chuyển hướng link zalo mở chat phần mềm zalo trên máy tính đúng số điện thoại

16/05/2025 614 lượt xem admin Cập nhật: 04/12/2025
3/5 - (218 bình chọn)
  • Chèn đoạn script này vào footer của website:

    • WP Admin → Giao diện → Tùy biến (Customize)Flatsome → Advanced → Global Settings → Custom Scripts → Dán vào ô “Footer Scripts”.

    • Hoặc dùng plugin như Insert Headers and Footers để dán vào Footer.

  • Không cần thay đổi nội dung các liên kết https://zalo.me/ đã có trên trang — script sẽ tự động xử lý đúng theo thiết bị.

    Đoạn JavaScript để tự động chuyển các link https://zalo.me/SĐT thành zalo://conversation?phone=SĐT khi người dùng dùng desktop, và giữ nguyên trên mobile.

    Dưới đây là đoạn mã JavaScript tối ưu:

    <script>
    document.addEventListener("DOMContentLoaded", function () {
      // Kiểm tra nếu là thiết bị di động
      const isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
    
    
      // Lấy tất cả các thẻ <a> có href bắt đầu bằng https://zalo.me/
      const zaloLinks = document.querySelectorAll('a[href^="https://zalo.me/"]');
    
    
      zaloLinks.forEach(function (link) {
        const href = link.getAttribute("href");
        const phoneMatch = href.match(/https:\/\/zalo\.me\/(\d{8,12})/);
        if (phoneMatch) {
          const phone = phoneMatch[1];
          if (!isMobile) {
            // Nếu là máy tính, đổi href thành zalo://...
            link.setAttribute("href", `zalo://conversation?phone=${phone}`);
    
    
            // Fallback: nếu app không mở, sau 2 giây trở lại zalo.me
            link.addEventListener("click", function (e) {
              setTimeout(function () {
                window.location.href = `https://zalo.me/${phone}`;
              }, 2000);
            });
          }
          // Nếu là mobile thì không thay đổi gì
        }
      });
    });
    </script>

     

Chào ! Bạn thấy nội dung này thế nào?
webdy.vn
webdy.vn

Tạo web nhanh dùng thử miễn phí.

Chia sẻ ↗

Bài liên quan

×