Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts

    load external css into a string?

    Hi

    Gidday

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    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.

  • Users who have thanked devnull69 for this post:

    shaunthomson (11-01-2012)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    use ajax to turn a file/url into a string:

    Code:
    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);
    	XHRt.send("");
    	return XHRt;
    }
    
    
    //usage:
    aGet("/style/screen.css", function(data){
        localStorage.myCSS=data;
        // do append to head style tags here
    });
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    shaunthomson (11-01-2012)

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Thanks guys. That function worked great rnd me!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •