0

I want to modify the CSS file using data that I get from the database. So, after the login, I am getting all the necessary data from DB and update styleSheet using insertRule/deleteRule methods then, redirect to the main page.

Login -> Theme engine page (modify css) -> home page

Theme engine page (theme.html) is an empty HTML page with one JS file (themeEngine.js) which modifies CSS file.

I checked the stylesheet in theme.html it is same as the expected result but, when it redirects to home page the CSS file goes back to its default version. The methods insertRule/deleteRule is not altering an actual file!

I tried importing themeEngine.js to every existing HTML file but in that case, default style appears (for a little amount of time, depending on the internet speed) before the theme engine starts to work and importing js file to every page is quite inconvenient.

I would like to know how can I solve this problem: having a custom style for every user. Is it possible to edit an actual CSS file using JavaScript?

3
  • 1
    You could just store the modifications on the client-side in a cookie or localstorage or something. It would only apply to their current machine and only until that data is persisted but it's the only thing you can do with client-side JS alone. You need some back-end thing if you want to store it somehow on the server. Commented Aug 21, 2019 at 11:30
  • 1
    When your backend knows about the loggedin user, can't you just add the individual styles to the view and deliver it then? Javascript manipulations are then not necassary. Commented Aug 21, 2019 at 11:42
  • for example at the first time you can show a overlay loading and then change the style ! Commented Aug 21, 2019 at 11:52

1 Answer 1

1

Browsers can't change data on a server without explicit support from it by the server. (Imagine how long the Google homepage would survive otherwise!)

Typically you would need to pick a server-side programming language, use it to write an API, and then interact with it using Ajax.

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

1 Comment

Is it possible to update static files during runtime using Java? I am using Spring-boot in the back-end.

Your Answer

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