想讓訪客在網站上一鍵關注或分享,各平台都提供了官方的 iframe 嵌入式按鈕。做法是把平台給的片段貼進頁面即可,不需要自己串 API。
<iframe width="120" height="24" frameborder="0" allowtransparency="true"
marginwidth="0" marginheight="0" scrolling="no" border="0"
src="https://widget.weibo.com/relationship/followbutton.php?language=zh_cn
&width=120&height=24&uid=1756720421&style=2&btn=red&dpc=1">
</iframe>
其中的 uid 要換成你自己的帳號 ID,style 與 btn 決定配色樣式。
<iframe src="https://open.qzone.qq.com/like?url=https://user.qzone.qq.com/1839458293 &type=button_num&width=120&height=24&style=3" allowtransparency="true" scrolling="no" border="0" frameborder="0" style="width:120px;height:24px;border:none;overflow:hidden;"> </iframe>
<iframe src="https://www.connect.renren.com/like?url=https://page.renren.com/699049347" style="width:140px;height:23px;" scrolling="no" frameborder="0" allowTransparency="true"> </iframe>
<iframe id="previewmc" height="24"
src="https://follow.v.t.qq.com/index.php?c=follow&a=quick
&name=yanghaonan7758&style=5&t=1336022961842&f=0"
frameborder="0" width="125" allowtransparency="true" scrolling="no">
</iframe>
https:// 的 iframe 會被瀏覽器判定為混合內容而直接阻擋。frameborder 與 allowtransparency 是舊 HTML 屬性,HTML5 中已不建議使用,改用 CSS 的 border:0 處理邊框。loading="lazy"。與其逐一嵌入各家按鈕,實務上常見的替代方案有兩種:一是使用 Web Share API,讓系統分享面板處理所有管道:
if (navigator.share) {
navigator.share({ title: document.title, url: location.href });
}
二是只放幾個主要管道的單純連結,例如 https://service/share?url=... 形式,不需要載入任何第三方腳本,效能與隱私都更好。