user5959252
user5959252

Reputation:

Is it possible to alter actual css file using javascript?

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?

Upvotes: 0

Views: 84

Answers (1)

Quentin
Quentin

Reputation: 943571

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.

Upvotes: 1

Related Questions