I can get the content of a single css file this way
getStyleCSS = $.get("MyStyle.css");
$.when(getStyleCSS).done(function(response) {
var strCSS = response; // response here is the text content of the css file
}
But when I try to get several css file contents like this I get Deferred objects instead of the css contents.
// Get a handle on an aspx created element which contains css script elements
var elementDiv = document.getElementById('somediv');
// Get the stylesheets from this element
var links= elementDiv.getElementsByTagName('link');
arrLinks = [];
arrDeferreds = [];
// jQuery get each of the css links
for (var i = 0; i < arrLinks.length; i++) {
var link = arrLinks[i];
arrDeferreds.push($.get(link));
}
// Fetch the css documents
$.when(arrDeferreds).done(function (response) {
var result = response; // response here is an array of Deferred objects
}
Is there a way to get the text content of multiple css files using javascript/jQuery?
Context: I need to send a subset of a page's HTML to an external (cross site) service which will generate a PDF of the specified HTML. In order for the pdf to be generated properly I need to embed the text of all css content into the html. The page contents originate with aspx/ascx controls and some of those controls contain script elements pointing to various css files. I'm trying to fetch the css text content of for each of these elements.
It's not really a solution (I still haven't figured out how to get the results from the Deferred objects) but this seems to be a legit work around. Thanks @AussieJoe for the pointer to another stackoverflow answer.
I can build up the css string for all css on the page like this
var strCSS = '';
for (var i = 0; i < document.styleSheets.length; i++) {
var stylesheet = document.styleSheets[i];
for (var j = 0; j < stylesheet.cssRules.length; j++) {
var rule = stylesheet.cssRules[j];
strCSS += rule.cssText;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With