添加此/共享此 VS “手动”添加每个按钮
我想在我的新页面(显示某个帖子时)添加一个类似 fb、google+1、tweet 按钮等。我知道例如 addthis 默认提供此功能。但缺点是你会得到一个有点大的 javascript 文件,偶尔会抛出一些错误。所以我的问题是:在类似的情况下,你们通常会选择哪种选择以及为什么。谢谢
i would like to add an fb-like, google+1, tweet-button etc to my new page (when displaying a certain post). I know that for example addthis offers this by default. But the downside is that you get a somewhat large javascript file that ocasionally throws some errors. So my question is: Which option and why do you guys usually go for in similar scenarios. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过 addthis 包含并不是我首选的包含社交按钮的方式。事实上,正如您所注意到的,有时会抛出一些错误,并且如果 addthis 库失败(例如 javascript 错误、服务器超时...)所有 按钮可能会失败,因为它们依赖于唯一的库。此外,出于简单的性能问题,addthis 是一个加载其他脚本的脚本(压缩后为 33kb),因此可以避免它,并且我可以完全控制社交按钮的包含。
我更喜欢异步加载所有需要的库,并仅在我确实需要它们时才注入必要的标记,就像这样
(为了性能,缓存对
.socialshare
的引用)loadscript
是一个简单的脚本加载器函数,基本上像这样,如果我的模板包含带有社交按钮的元素(
.socialshare
元素),我会注入带有 id 的脚本元素(以便我确信没有其他脚本可以将它们注入两次)The inclusion via addthis is not my preferred way to include social buttons. In fact as you noticed, sometimes some errors are thrown and if the addthis library fails (e.g. javascript error, server timeout...) all buttons could fail, since they depend by a unique library. Beside, for a simple matter of performance, addthis is a script who loads other scripts (it's 33kb gzipped) so it can be avoided and I can take full control of social buttons inclusion.
I prefer load asynchronously all the needed libraries and inject the necessary markup only if I really need them, like this
(for the sake of performance, cache a reference to
.socialshare
)loadscript
is a simple script loader function like thisbasically if my template contains the element with social button (the
.socialshare
element) I inject the script element with an id (so that I'm sure no other script can inject them twice)