Whatsapp:无法与 Javascript 共享当前 link
Whatsapp: Un able to share current link with Javascript
我正在尝试使用以下 javascript 和 HTML
在 whatsapp 中分享当前的 link
<script language="javascript">
function waCurrentPage(){
return "whatsapp://send?text=Check this out: "+'http://' +
window.location.hostname + window.location.pathname;
}
</script>
<a class="btn btn-social-icon btn-whatsapp" href="javascript:waCurrentPage()"
data-action="share/whatsapp/share"><i class="fa fa-whatsapp"></i>
</a>
我不知道为什么它不起作用我在按下按钮后在浏览器中得到这个输出:
whatsapp://send?text=查看:http://bggressive.nl/test/index.html
试试这个:
<a class="btn btn-social-icon btn-whatsapp" href="javascript:window.location=waCurrentPage();">Link</a>
JS:
waCurrentPage = function() {
return encodeURI("whatsapp://send?text=Check this out: " + 'http://' + window.location.hostname + window.location.pathname);
}
我知道这比您想要的要冗长一些,但它确实有效,您还可以添加自定义 css。
$(document).ready(function() {
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
$(document).on("click", '.whatsapp', function() {
if( isMobile.any() ) {
var text = $(this).attr("data-text");
var url = $(this).attr("data-link");
var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
var whatsapp_url = "whatsapp://send?text=" + message;
window.location.href = whatsapp_url;
} else {
alert("Please share this article in mobile device");
}
});
});
下面是将当前 url 转发到 Whatsapp API:
的示例函数
function getURL() {
open(encodeURI("https://api.whatsapp.com/send?text="+window.location.href));
}
<button type="button" onclick="getURL();">Whatsapp</button>
我正在尝试使用以下 javascript 和 HTML
在 whatsapp 中分享当前的 link<script language="javascript">
function waCurrentPage(){
return "whatsapp://send?text=Check this out: "+'http://' +
window.location.hostname + window.location.pathname;
}
</script>
<a class="btn btn-social-icon btn-whatsapp" href="javascript:waCurrentPage()"
data-action="share/whatsapp/share"><i class="fa fa-whatsapp"></i>
</a>
我不知道为什么它不起作用我在按下按钮后在浏览器中得到这个输出:
whatsapp://send?text=查看:http://bggressive.nl/test/index.html
试试这个:
<a class="btn btn-social-icon btn-whatsapp" href="javascript:window.location=waCurrentPage();">Link</a>
JS:
waCurrentPage = function() {
return encodeURI("whatsapp://send?text=Check this out: " + 'http://' + window.location.hostname + window.location.pathname);
}
我知道这比您想要的要冗长一些,但它确实有效,您还可以添加自定义 css。
$(document).ready(function() {
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
$(document).on("click", '.whatsapp', function() {
if( isMobile.any() ) {
var text = $(this).attr("data-text");
var url = $(this).attr("data-link");
var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
var whatsapp_url = "whatsapp://send?text=" + message;
window.location.href = whatsapp_url;
} else {
alert("Please share this article in mobile device");
}
});
});
下面是将当前 url 转发到 Whatsapp API:
的示例函数function getURL() {
open(encodeURI("https://api.whatsapp.com/send?text="+window.location.href));
}
<button type="button" onclick="getURL();">Whatsapp</button>