首页 > 解决方案 > 如何在手机网站上制作转发按钮,使用内部转发功能

问题描述

我需要在网站上实现当前页面链接共享按钮,我尝试使用 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>

标签: javascripthtmlfrontend

解决方案


在 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


推荐阅读