Twitter,Facebook,G +共享外部javascript文件中的按钮

时间:2012-04-30 10:32:22

标签: javascript facebook twitter bookmarks social

如果我需要标准社交分享按钮放在外部JavaScript中,然后填入页面内容,该怎么办?默认按钮基本上是容器和javascript代码。

将所有内容放入一个javascript文件中的正确方法是什么?下面我以我看到的方式提供我当前的JAVASCRIPT版本,但我遇到的问题是google plus在这种情况下没有出现在IE中,所以我想也许在哪里有更好的方法:

STANDAR HTML VERSION:

<!--social-->
<div id="social">

<!--twitter-->
<div>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="user" data-lang="en" data-count="vertical">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
<!--/twitter-->

<!--gplus-->
<div>
<g:plusone size="tall"></g:plusone>
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>
</div>
<!--/gplus-->

<!--facebook-->
<div>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-layout="box_count" data-href="yourlink" data-width="42" data-show-faces="false"></div>
</div>
<!--/facebook-->

</div>
<!--/social-->

JAVASCRIPT VERSION

<script>

function socialBookmarks(){

//containers
var socialString = '<!--social--><div id="social"><!--gplus--><div><g:plusone size="tall"></g:plusone></div><!--/gplus--><!--twitter--><div><a href="https://twitter.com/share" class="twitter-share-button" data-via="user" data-lang="en" data-count="vertical">Tweet</a></div><!--/twitter--><!--facebook--><div><div id="fb-root"></div><div class="fb-like" data-layout="box_count" data-href="yourlink" data-width="42" data-show-faces="false"></div></div><!--/facebook--></div><!--/social-->';

//where to populate bookmars
var divContainer = document.getElementById('container');
divContainer.style.position = 'relative';
var divTag = document.createElement("div");
divTag.style.position = "absolute";
divTag.style.top = "0";
divTag.style.left = "-152px";           
divTag.innerHTML = socialString;    
document.getElementById('container').appendChild(divTag);


//Core social functions

//twitter
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

//gplus
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();

//fb
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


}

//Lets wait for page get loaded
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

//pull socialbookmarks function
addLoadEvent(function(){
    socialBookmarks();
});

</script>

0 个答案:

没有答案