html - Detect when dynamically injected CSS is loaded -
we have image-heavy site, order images load important.
one piece of js injecting <link type="text/css" ...> tags css, goes on preload images injecting <img> tags. trouble want images referenced css load before preloaded images, isn't happening. downloads css, preloaded images, , images referenced css.
what can other setting timeout? (which short people , long others)
in general can control loaded when javascript. trouble don't know exactly, when dynamically loaded css ready, can't decide when load what.
some browsers raise on-load event when such dynamically loaded css ready, there issues approach. spent few hours looking solution myself. here came in "fluxx compensator (y)", app owncloud system:
oc.fluxx.transitions=$('<link/>',{ 'rel':'prefetch', 'type':'text/css', 'href':oc.filepath('fluxx_compensator','css','transitions.css') toggle: function(handle){ // temporarily include transition style rules if not yet present (should not be!) var transitions=oc.fluxx.transitions.clone().attr('rel','stylesheet').attr('id','fluxx-transitions').appendto('head'); // safety catch browsers not fire load event when stuff loaded (safari) var timer = settimeout(function(){$('head #fluxx-transitions').off('load');oc.fluxx.time(handle);},500); // should preloaded... // more elegant approach react on load event (_if_ fired) $('head #fluxx-transitions').one('load',function(){cleartimeout(timer);oc.fluxx.time(handle);}); } // oc.fluxx.toggle as can see clone object (defined when loading page) represents reference css resource. preload have clone , delete when required (several times in case) instead of having load via network. place clone head of document. setup timer safety catch try react on on load event. if event not raised (for example safari under ms-windows not raise it), @ least timeout catches. best combination of fast reaction (event driven) , safety (timeout) find. works fine far...
hope helps ;-)
Comments
Post a Comment