1

I'm planning to make web GUI components which will be using jquery. so for modulity purpose it is safe to define component's styles inside jquery?

2 Answers 2

2

Safe? Yes - recommended - probably not.

I would say that it is always better to use a modular approach and separate "functionality" appropriately.

Your JavaScript code should be inside .js files and your style definitions should be in .css files (or some other css preprocessor format). This way you know exactly where to find everything.

The instances where it would make sense to handle CSS within your JS would be if all the CSS styles of the elements change dynamically at run time or if you are required to only have one file per component in which case you should make sure to keep all of your CSS manipulations in one central location within the code - for example in a single function.


To summarize: It's probably not "safe" to handle all style definitions within your JavaScript purely because it might cause confusion for other/future developers.

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

Comments

0

I recommend that you use external stylesheets because css and javascript are loaded simultaneously. Defining styles in the script will make both the operations slower as the operations will take place one by one rather than the same time.

1 Comment

"css and javascript are loaded simultaneously" - it is my understanding that there is a limited number of simultaneous requests (per domain) that a browser can make. The files are not intrinsically loaded simultaneously - consider an instance whereby a page has 20 JS files and only 1 CSS file - you can't guarantee that they will be loaded in the same batch of requests...

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.