如何制作手机网站上的转发按钮,使用内置的转发功能



我需要在网站上实现当前页面链接共享按钮,我试图使用webshare api来实现,但在所有设备上我都收到了浏览器版本过期的警报,我需要更新它。你能建议其他方法吗?

var shareButton = document.getElementById('share-button');
shareButton.addEventListener('click', function () {
if (navigator.share) {
console.log("Your browser support")
navigator.share({
title: "title",
text: "text",
url: window.location.href,
})
.then(function () {
console.log("Success")
})
.catch(function () {
console.log("Error")
})
} else {
alert("Your browser version is out of date")
}
})'''
<div class="share-button-plugin mt-30">
<button id="share-button"><i class="fa fa-share-alt"></i>Share</button>
</div>

在Firefox网站上(https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share(写的应该使用navigator.canShare()

此外,还有一个注意事项:

安全上下文。此功能仅在安全上下文(HTTPS(、某些或所有支持浏览器中可用。

由于浏览器兼容性差,我一般不会使用此功能:

https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#Browser_compatibility

最新更新