javascript - Why "document.styleSheets" returns empty value after each 3 to 5 browser refreshes? -



javascript - Why "document.styleSheets" returns empty value after each 3 to 5 browser refreshes? -

what want accomplish create user define specific stylesheets processed javascript file wrote , here code used specified stylesheets :

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>document</title> <link rel="stylesheet" href="sheet-1.css"> <link rel="stylesheet" href="sheet-2.css"> <link rel="stylesheet" href="sheet-3.css"> <!-- other css files --> <style> /* inline styles */ </style> </head> <body> <script src="jquery-2.1.1.min.js"></script> <!-- plugins --> <script> jquery(document).ready(function($) { // user specified stylesheets var watchedstylesheetsnames = [ 'sheet-1.css', 'sheet-2.css', 'sheet-3.css' ] var allsheets = document.stylesheets, targetsheets = []; ( var = watchedstylesheetsnames.length - 1; >= 0; i--) { object.keys( allsheets ).foreach ( function (j) { if( allsheets[j].href && allsheets[j].href.indexof( watchedstylesheetsnames[i] ) !== -1 ) { targetsheets.push(allsheets[j]); } }); }; // result console.log( 'watched stylesheets array : ' + targetsheets ); }); </script> </body> </html>

the problem works fine first time page load , after 3 5 refreshes array of targetsheets returns empty value ruins done after after refreshing time expected value , on. tried browsers, cleared cache same thing happens 1 time again , again. here image of in console:

https://www.dropbox.com/s/ipnc20hcdr3d6wl/01.png?dl=0

i tested code online & locally using wamp server latest versions of browsers. causes problem , how can prepare ? time.

edit found reply because implementing prefixfree plugin remove link nodes , reinsert processed css in style tags.

you're looking stylesheets within jquery ready handler. whole point of jquery ready handler fires after html has been parsed before dependencies have been downloaded , evaluated. apparently sometimes, code runs before stylesheets have been loaded.

you'll want wait until stylesheets have been loaded. way know of check whether linked stylesheet has been loaded load dynamically, looking load event before setting href. there may other ways, that's 1 know.

in case, instance, remove <link> elements markup , this:

jquery(document).ready(function($) { // user specified stylesheets var watchedsheets = [ { name: 'sheet-1.css' }, { name: 'sheet-2.css' }, { name: 'sheet-3.css' } ]; var completed = 0; var head = document.queryselector('head'); watchedsheets.foreach(function(sheet, index) { var link = document.createelement('link'); link.onload = function() { sheet.loaded = true; ++completed; if (completed === watchedsheets.length) { done(); } }; link.onerror = function() { sheet.error = true; ++completed; if (completed === watchedsheets.length) { done(); } }; link.rel = "styleshet"; link.href = sheet.name; head.appendchild(link); }); function done() { var allsheets = document.stylesheets, targetsheets = []; object.keys( allsheets ).foreach ( function (j) { var sheet = watchedsheets[j]; if( !sheet.error && allsheets[j].href && allsheets[j].href.indexof( sheet.name ) !== -1 ) { targetsheets.push(allsheets[j]); } }); // result console.log( 'watched stylesheets array : ' + targetsheets ); } });

javascript jquery dom browser

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -