前端分享 || facebook,linkedin,twitter,浏览器自带

// facebook,linkedin,twitter分享
let obj = {
  href: "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(window.location.href),
  target: "_blank",
  specs: "toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=600, height=450,top=100,left=350"
};
switch (value) {
  case "facebook":
    obj.href = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(window.location.href);
    break;
  case "linkedin":
    obj.href = "http://www.linkedin.com/shareArticle?mini=true&url=" + encodeURIComponent(window.location.href);
    break;
  case "twitter":
    obj.href = "https://twitter.com/intent/tweet?text=&url=" + encodeURIComponent(window.location.href);
    break;
  default:
    break;
}
window.open(obj.href, obj.target, obj.specs);

还需要以下的内容在<head></head>里边加(时间久了有点忘了需要重新试下)

<meta property="og:url" content="分享链接的url" />
<meta property="og:description" content="分享的description" />
<meta property="og:image" content="分享图片的url" />
<meta property="og:title" content="分享内容的title" />

浏览器自带的分享

let title = document.querySelector("title") ? document.querySelector("title").innerText : "";
    let text = document.querySelector('meta[name="description"]') ? document.querySelector('meta[name="description"]').getAttribute("content") : "";
    let url = window.location.href;
    if (navigator.share) {
      navigator
        .share({
          title: title,
          text: text,
          url: url
        })
        .then(() => {})
        .catch(console.error);
    }

浏览器自带的分享适配

Navigator.share - Web API 接口参考 | MDN (mozilla.org)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值