在网页中添加社交媒体资源时,传统方法会加载大量来自其他域的外部JS,只是为了加载一个iframe或带有品牌形象背景的 anchor 。以下可能是网络上传输最多的文件(在 http://www.google-analytics.com/ga.js 36.35KB 之后,这对许多人来说是不可避免的)
http://connect.facebook.net/en_US/all.js 181.30KB(压缩后 59.06KB)
https://platform.twitter.com/widgets.js 75.19KB(压缩后为 24.42KB)
https://apis.google.com/js/plusone.js 16.71KB
http://assets.pinterest.com/js/pinit.js (虽然这很小,但仍然不需要连接)
For example, http://connect.facebook.net/en_US/all.js does only one thing: adding an iFrame
<iframe src="//www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Flavishdream&width=292&height=180&colorscheme=light&show_faces=true&border_color&stream=false&header=false" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:292px; height:180px;" allowtransparency="true"></iframe>
Twitter and Google Plus scripts does very similar tasks, only adding small HTML chunks into the page.
为什么不只编写这些 iFrame、图像和 anchor HTML?
最佳答案
您提到的大多数脚本也在后台跟踪用户行为。此外,当加载 JS 文件时,提供商(例如 Google)可以更改此类脚本附带的功能。当您只是添加静态文件或自己编写所需的 HTML 时,这并不那么容易。
关于javascript - 将社交媒体的外部Javascript加载到网页中的目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916045/