View Full Version : load external css into a string?

10-30-2012, 01:37 PM


I'm aiming to save external css in localStorage like Bing and Google do.

I'm learning localstorage, and so far I've set up code to check if I have a variable called 'myCSS' stored in localstorage, can successfully load it into JS, or if it's not there, populate it with some inline css eg body{background-color:#000}

The next step I've completed is taking this css and appending it into a style tag in the head, which works.

My final aim is to use external css instead of inline. How do I put an external css file into a string?

Thanks for your help.

10-30-2012, 03:05 PM
External CSS can only be files on a server! So the only thing you could do at client side is to store the URL to the external CSS file and then replace the href filename of the <link> tag.

rnd me
10-31-2012, 06:18 AM
use ajax to turn a file/url into a string:

function aGet(turl, callback) {
var XHRt = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
XHRt.onreadystatechange = function () {if (XHRt.readyState == 4 && XHRt.status == 200) {callback(XHRt.responseText, XHRt);}};
XHRt.open("GET", turl, true);
return XHRt;

aGet("/style/screen.css", function(data){
// do append to head style tags here

11-02-2012, 04:22 AM
Thanks guys. That function worked great rnd me!!!