是有可能的几个异步社交按钮装载机合并成一家? [英] is it possible to unite several async social button loaders into one?

查看:102
本文介绍了是有可能的几个异步社交按钮装载机合并成一家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我这3个社交按钮在页面的结尾。我在想,重写相同功能的3倍是不是很聪明。我能团结他们?它的工作?

任何想法如何做到这一点?在逻辑上,我会做:
g.src ='http://apis.google.com/js/plusone.js','http://connect.facebook.net/en_US/all.js#xfbml=1,HTTP://平台.twitter.com / widgets.js';

但我不知道这是否正确的方法做...

 <脚本类型=文/ JavaScript的>
//<![CDATA [
(函数(D,T){
变种G = d.createElement(T)
S = d.getElementsByTagName(t)的[0];
g.async = TRUE;
g.src ='http://apis.google.com/js/plusone.js';
s.parentNode.insertBefore(G,S);
})(文件,'脚本');
//]]>
< / SCRIPT>
<脚本类型=文/ JavaScript的>
//<![CDATA [
(函数(D,T){
变种G = d.createElement(T)
S = d.getElementsByTagName(t)的[0];
g.async = TRUE;
g.src ='http://connect.facebook.net/en_US/all.js#xfbml=1';
s.parentNode.insertBefore(G,S);
})(文件,'脚本');
//]]>
< / SCRIPT>
<脚本类型=文/ JavaScript的>
//<![CDATA [
(函数(D,T){
变种G = d.createElement(T)
S = d.getElementsByTagName(t)的[0];
g.async = TRUE;
g.src ='http://platform.twitter.com/widgets.js';
s.parentNode.insertBefore(G,S);
})(文件,'脚本');
//]]>
< / SCRIPT>


解决方案

  VAR脚本= [\"http://apis.google.com/js/plusone.js\",\"http://connect.facebook.net/en_US/all.js#xfbml=1\",\"http://platform.twitter.com/widgets.js\"];
(函数(阵){
   对于(VAR I = 0,LEN = array.length; I< LEN,我++)
   {
      VAR ELEM =使用document.createElement('脚本');
      elem.type =文/ JavaScript的';
      elem.async = TRUE;
      elem.src =阵列[我]
      变种S = document.getElementsByTagName('脚本')[0];
      s.parentNode.insertBefore(ELEM,S);
   }
})(脚本);

这是我的解决方案,在Linux下的浏览器和Mozilla测试!

So far I've these 3 social buttons at the end of the page. I was thinking that rewriting same function 3 times isn't very smart. Could I unite them? would it work?

Any ideas how to do this? logically I would do: g.src = 'http://apis.google.com/js/plusone.js', 'http://connect.facebook.net/en_US/all.js#xfbml=1', 'http://platform.twitter.com/widgets.js';

but I'm not sure if this correct way to do it...

<script type="text/javascript">
//<![CDATA[
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = true;
g.src = 'http://apis.google.com/js/plusone.js';
s.parentNode.insertBefore(g, s);
})(document, 'script');
//]]>
</script>


<script type="text/javascript">
//<![CDATA[
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = true;
g.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
s.parentNode.insertBefore(g, s);
})(document, 'script');
//]]>
</script>


<script type="text/javascript">
//<![CDATA[
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = true;
g.src = 'http://platform.twitter.com/widgets.js';
s.parentNode.insertBefore(g, s);
})(document, 'script');
//]]>
</script>

解决方案

var scripts = ["http://apis.google.com/js/plusone.js","http://connect.facebook.net/en_US/all.js#xfbml=1","http://platform.twitter.com/widgets.js"];
(function(array) {
   for (var i = 0, len = array.length; i < len; i++)
   {
      var elem = document.createElement('script');
      elem.type = 'text/javascript';
      elem.async = true;
      elem.src = array[i];
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(elem, s);
   }
})(scripts);

This is my solution, tested on Chrome and Mozilla under Linux!

这篇关于是有可能的几个异步社交按钮装载机合并成一家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆