javascript – Save css styling in JS to browser cache-ThrowExceptions

Exception or error:

I am new to JS. I am wondering if is possible to save css styling in javascript temporary or permanently in user broswer cache.

How to solve:

Standard HTTP caching works fine for CSS.

There’s no need to involve JavaScript at all. You could use a Service Worker to implement some custom caching rules instead of using standard HTTP caching but that would be a lot of complexity and likely no benefits.


Re comment:

$(document.head).append('<style>#button {pointer: none;}</style>'); but when i refresh the page the style is gone.

That wouldn’t be regular caching, and isn’t really anything to do with CSS either. That would be preserving changes to the DOM made by JavaScript.

There’s no trivial way to do that. A general approach would be to keep a record of the changes you make to the DOM, store it somewhere (such as local storage), and then use the load or DOMContentLoaded event to call a function that replays the changes you made to the DOM.

Leave a Reply

Your email address will not be published. Required fields are marked *