In your example, this should be var retrievedObject = JSON.parse(localStorage.getItem('testObject')) Since localStorage can only process pairs of row values, itβs now worth trying to create a new object as follows:
{type: "css", blob: "<serverside rendered>", deferred: false}
Here is an example:
var resources = [ {type: "css", blob: ".unknown { color: red; } div { margin: 20px; }", deferred: false} ] function appendResources(){ var head = document.getElementsByTagName("head")[0]; for(var resourceIndex in resources){ var resource = resources[resourceIndex] if(resource.type == "css"){ var element = document.createElement("style"); element.type = "text/css"; element.appendChild(document.createTextNode(resource.blob)); head.appendChild(element) } } } document.getElementsByTagName("button")[0].addEventListener("click", appendResources);
<div class="unknown">Defaultly, i have no style.</div> <button>Add style from localStorage</button>
You can populate an array of objects like these with your server side implementation and then add them to dom. This example only handles CSS and does not make any difference to the deferrable elements, as it adds everything to the <head> , but you get the idea.
I copied the interesting part of this answer: fooobar.com/questions/59500 / ...
These objects can be stored in localStorage using: localStorage.setItem('resources', JSON.stringify(resources)); and then use again.
<h / ">
Update
I feel that the answer to your question can be much simpler. If your .css and .js files are static, you can simply compose an array of external resources (URLs) and add these URLs to <head> and <body> later, once you have downloaded them from localStorage . This will make these inline styles and javascript useless, and it will be a much cleaner solution.
source share