如果安裝了whatsapp客戶端,只需跳轉
window.location.href = "https://wa.me/號碼";
如果有區號則只需在前面直接加區號,例如加香港區號:
https://wa.me/852XXXXXXXX
if (window.navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { window.location.href = "https://wa.me/號碼"; setTimeout(function () { window.location.href = "https://itunes.apple.com/us/app/whatsapp-messenger/id310633997?mt=8"; window.location.href = "https://itunes.apple.com/us/app/whatsapp-messenger/id310633997?mt=8"; }, 2000); } else if (window.navigator.userAgent.match(/android/i)) { window.location.href = "https://wa.me/號碼";; setTimeout(function () { window.location.href = "https://play.google.com/store/apps/details?id=com.whatsapp"; }, 2000) }
或者可以到 http://www.conversabit.com/wasap.js/ 提供的小插件自動適配
html:
<span data-whatsapp="5215585420898">This will be clickable on mobile</span>
javascript:
wasap.init()
插件還能配合vue應用,具體查看官網