I’ve been tweaking my website lately to make it load a bit faster.
I stumbled across an old article to defer external javascript and I thought I might apply it with the facebook like button seeing as it takes its time to load.
So I created a new js file, called it fbdefer and added this to it:
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.async=true; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=294326793972731";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
I then added it to the header of my wordpress with:
<div id="fb-root"></div>
<script type="text/javascript">
function deferJSWordPress() {
var element = document.createElement("script");
element.src = "/fbdefer.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", deferJSWordPress, false);
else if (window.attachEvent)
window.attachEvent("onload", deferJSWordPress);
else window.onload = deferJSWordPress;
</script>
It all works fine, and it loads the buttons after the page has loaded. But when I do speed tests, they all return 404 errors relating to that file.
Any clues?
Thank you