I'm trying to load my non-essential CSS through JS using the code Google provides on their page speed analytics site. The code is:
var cb = function() {
var l = document.createElement('link'); l.rel = 'stylesheet';
l.href = <?php echo "'css/" . $page . "-mobile.css'"; ?>;
var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
};
var raf = requestAnimationFrame || mozRequestAnimationFrame ||
webkitRequestAnimationFrame || msRequestAnimationFrame;
if (raf) raf(cb);
else window.addEventListener('load', cb);
It seems to work well on safari (desktop and mobile), chrome (desktop and mobile) and mozilla (desktop and mobile). But for some reason the browser Samsung uses for Galaxy S4 and below is not loading the css file (though S5 works as predicted, and loads the CSS).
I tried everything and I just think that the built-in browser samsung uses is just crappy.. Would love for any suggestion that might help here, Thanks, Boaz.
linkelement before theheadelement. Should it? Try replacingh.parentNode.insertBeforewithh.appendChild. Or if your css file must be the inserted as the first element ofhead, tryh.insertBefore(l, h.firstChild).else window.addEventListener('load', cb);withelse cb();? I don't see any worthwhile benefit to deferappendChilduntil page load is complete. If that doesn't satisfy iOS either, then trydocument.body.appendChildand see what happens.