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,398
    Thanks
    12
    Thanked 570 Times in 563 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,448
    Thanks
    11
    Thanked 598 Times in 578 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 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • 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
    •