0

Following problem - I stored a stylesheet in the local storage. But when I try to load it from there I use JSON.parse() and it seems that the browser will already render the CSS. But I would prefer the browser to treat the parsed CSS as text, then append the text to a style tag.

The Code:

loading the stylesheet from server and store it in local storage

function load_css(url){
        $.ajax({
            url: url,
            cache: false,
            dataType: 'text',       
            success: function(res){ 
                $('<style>').attr('id', 'my_style').text(res).prependTo('#my_div');         
                if (typeof(Storage) !== "undefined" && typeof localStorage !== "undefined" && localStorage !== null && typeof localStorage.setItem === 'function') {
                    localStorage.setItem("my_style", JSON.stringify(res));                                  
                }                           
            },
            error: function(err){
                console.log(err);
            }
        }); 
    }

within another function the styles shall be loaded from local storage if set:

try{
    if (typeof(Storage) !== "undefined" && typeof localStorage !== "undefined" && localStorage !== null && localStorage.getItem("my_style") !== null) {
        var css = JSON.parse(localStorage.getItem('my_style'));
        $('<style>').attr('id', 'my_style').text(css).prependTo('#my_div');
        ... //load style from server if local storage not available, item not found

Even when I leave out the line with the created style tag the css is being rendered. With this line I see that if I inspect an element the element styles appear twice in the developer tool bar. So how could I prevent the browser from rendering the styles when using JSON.parse(). Maybe there is another method that treats the styles from local storage as plain text?

4
  • Styles shouldn't render until they are in the document. I suspect maybe prependTo('#my_div') inserts the style tags in the HTML? In that case that is what makes them render. Alternatively it might be some behind-the-scenes magic from jQuery, in which case you will have to handle your styles without it. Maybe use a documentFragment? Commented Oct 28, 2016 at 11:02
  • Thanks for your comment. But as I said - even with removing the line that creates the style tag the CSS is being rendered. I think after the JSON.parse the browser recognizes that it's CSS and renders it. But I need to use JSON.parse otherwise it's just stringified nonsense which I can't prepend anywhere ;) Commented Oct 28, 2016 at 11:07
  • can you show the text that make up 'my_style'? Commented Oct 28, 2016 at 11:12
  • Do you mean the content of the CSS file? It's too long to write it down here. But if you mean the line that creates the style tag it's the line from above: $('<style>').attr('id', 'my_style').text(css).prependTo('#my_div'); Commented Oct 28, 2016 at 11:14

2 Answers 2

3

You are calling this

$('<style>').attr('id', 'my_style').text(res).prependTo('#my_div');   

before storing it. So it's on the document once you get it through ajax, and then on the other code you just put it again.

Sign up to request clarification or add additional context in comments.

5 Comments

Thx! But no. Sorry for the misunderstanding. But I left out the check. The load_css is only called when the file is not in the local storage. The load css function is never fired when the css is retrieved from local storage. It's not about that, I already checked it. The CSS is rendered when calling JSON.parse() and another time when creating the style tag.
I think the problem Is in another place. If I do some simplified version of this, parsing json doesn't apply the css. Anyway, why use JSON? the string is just css code right? If it was an object this wouldn't work.
Yes, in the meanwhile I also tried to store it unstringified. And it works but still rendering the CSS twice...
When does the css get rendered first? put interruption points on places and see when does it happen. If you don't stringify means that you don't call Json.parse, then where is it that the first css is inserted in the document?
Thanks you very much for your tips. Problem solved. In the end it was something really stupid. I used an old version of a template because a merge went wrong and didn't see that there was an old inline embed of the resource within the jsp template. The jsp line created an inline style tag and I oversaw it. Without the breakpoints I couldn't see it... Well, sometimes it's this stupid :D
0

Sorry to write here as I do not have sufficient reps to comment I am writing it down here.

I am just wondering why would you want to load your stylesheet from local storage. We already have a browser cache for that. Then what is the benefit/purpose of doing it using local storage?

I am just having a curiosity.

2 Comments

Excellent question! And to be honest I don't have a good answer for you. The browser cache is limited (~12MB) and if the user uses the browser heavily the cache will be overwritten. Also many people clear cache pretty often. local storage persists longer I guess. For me it's just a performance experiment. Because it's about a large javascript file and a large css file (base64 images embedded) that are rarely changed but probably will be called pretty often.
Oh...I understand..If it embeds base64 images, using local storage is preferred approach. Well thanks.!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.