如何修改代码实现网页访问时自动弹出WhatsApp聊天窗口?
Hey there! Your existing code is almost ready to go—you just need to add one key configuration to make the chat popup trigger automatically when someone visits your page. Let’s walk through the fix:
The Quick Fix: Add autoOpenTimeout
The floatingWhatsApp plugin has an autoOpenTimeout parameter that controls how long (in milliseconds) to wait before opening the popup automatically. By default, this setting isn’t enabled, so adding it will make the popup show right away.
Here’s your updated, fully functional code with the critical change highlighted:
<!DOCTYPE html> <html> <head> <title>Auto-Open WhatsApp Chat</title> <style> body { font-family: Arial, Helvetica, sans-serif; } </style> <!-- Floating WhatsApp CSS --> <link rel="stylesheet" href="https://rawcdn.githack.com/rafaelbotazini/floating-whatsapp/3d18b26d5c7d430a1ab0b664f8ca6b69014aed68/floating-wpp.min.css"> <!-- jQuery (must load before the plugin script) --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> </head> <body> <!-- WhatsApp Render Container --> <div id="WAButton"></div> <!-- Floating WhatsApp JavaScript --> <script type="text/javascript" src="https://rawcdn.githack.com/rafaelbotazini/floating-whatsapp/3d18b26d5c7d430a1ab0b664f8ca6b69014aed68/floating-wpp.min.js"></script> <script> $(function() { $('#WAButton').floatingWhatsApp({ phone: '1231231231', // WhatsApp business number (international format) headerTitle: 'Chat with us on WhatsApp!', // Popup header title popupMessage: 'Hello, how can we help you?', // Popup initial message showPopup: true, // Enable popup display autoOpenTimeout: 0, // **Add this line: 0 = open immediately on page load** buttonImage: '<img src="https://rawcdn.githack.com/rafaelbotazini/floating-whatsapp/3d18b26d5c7d430a1ab0b664f8ca6b69014aed68/whatsapp.svg" />', // Button icon position: "right" }); }); </script> </body> </html>
Quick Notes to Keep in Mind:
- Set
autoOpenTimeout: 0for an immediate popup. If you want a small delay (like 2 seconds to let the page load fully), use2000instead. - Ensure jQuery loads before the floating WhatsApp script—this prevents plugin initialization errors.
- The
#WAButtoncontainer must exist in your HTML (you already have this covered in your original code!).
That’s all you need! Now when someone lands on your page, the WhatsApp chat popup will pop up automatically without needing a button click.
内容的提问来源于stack exchange,提问作者Ahmad Sulaeman

