$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Social Plugins (Fb, Twitter, Google) adding 3 seconds of load time to my site. Defer them? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Social Plugins (Fb, Twitter, Google) adding 3 seconds of load time to my site. Defer them? - Stack Overflow

matteradmin5PV0评论

I know you can use the defer="" html attribute to defer javascript files, but it's only supported in IE (lol) and that will only defer javascript files, I need to defer the entire plugin from loading.

Is there any way to do this, at all? I love the Facebook integration and such, but the plguins are SO SLOW. It more than doubles the load time.

Thanks! ~ Jackson

<div class="socialplugins"><a href="" class="twitter-share-button" data-count="vertical" data-via="DesignSweeter">Tweet</a><script type="text/javascript" src=".js"></script>
</div>

<div class="socialplugins">
<div id="fb-root"></div><script src=".js#appId=218305128208494&amp;xfbml=1"></script><fb:like href="/" send="false" layout="box_count" width="55" show_faces="true" font=""></fb:like>
</div>

<div class="socialplugins">
<script type="text/javascript" src=".js"></script>
<g:plusone size="tall" count="true"></g:plusone>
</div>

NOTE: The .socialplugins class is just for positioning in my header.

I know you can use the defer="" html attribute to defer javascript files, but it's only supported in IE (lol) and that will only defer javascript files, I need to defer the entire plugin from loading.

Is there any way to do this, at all? I love the Facebook integration and such, but the plguins are SO SLOW. It more than doubles the load time.

Thanks! ~ Jackson

<div class="socialplugins"><a href="http://twitter./share" class="twitter-share-button" data-count="vertical" data-via="DesignSweeter">Tweet</a><script type="text/javascript" src="http://platform.twitter./widgets.js"></script>
</div>

<div class="socialplugins">
<div id="fb-root"></div><script src="http://connect.facebook/en_US/all.js#appId=218305128208494&amp;xfbml=1"></script><fb:like href="http://designsweeter./" send="false" layout="box_count" width="55" show_faces="true" font=""></fb:like>
</div>

<div class="socialplugins">
<script type="text/javascript" src="https://apis.google./js/plusone.js"></script>
<g:plusone size="tall" count="true"></g:plusone>
</div>

NOTE: The .socialplugins class is just for positioning in my header.

Share Improve this question edited Jul 27, 2011 at 4:41 alt asked Jul 27, 2011 at 4:34 altalt 14k21 gold badges82 silver badges124 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

Load javascript files dynamically in non-blocking fashion:

http://berklee.github./nbl/

or

https://github./rgrove/lazyload/

This technique works somewhat like this:

 var script = document.createElement("script");
 script.type = "text/javascript";
 script.src = "file1.js";
 document.getElementsByTagName("head")[0].appendChild(script);

This new element loads the source file file1.js. The file begins downloading as soon as the element is added to the page. The important thing about this technique is that the file is downloaded and executed without blocking other page processes, regardless of where the download is initiated. You can even place this code in the of a document without affecting the rest of the page (aside from the one HTTP connection that is used to download the file).

this book: "High Performance JavaScript" by Nickolas Zakas has a lot of interesting information about JavaScript performace optimization.

Two solutions, create a script element (it's going to load the JS asynchronously):

var scriptElem = document.createElement('script'); 
scriptElem.src = 'http://anydomain./A.js'; 
document.getElementsByTagName('head')[0].appendChild(scriptElem);

And also writing the script tag directly:

document.write("<script type='text/javascript' src='A.js'><\/script>");

Both examples are from the Even Faster Websites book by Steve Souders.

/*
* Google Plus
* http://www.google./intl/en/webmasters/+1/button/index.html
* */
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google./js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

/*
* Facebook
* https://developers.facebook./docs/reference/plugins/like/
* */
(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/en_US/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

/*
 * Twitter
 * http://twitter./about/resources/buttons
 * */
!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./widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

Move all of your tags to the end of your page, right above </body> tag. That should prevent the in-line scripts from blocking UI, etc.

Post a comment

comment list (0)

  1. No comments so far