views:

87

answers:

3

Hey all,

I'm making this search component that I can just load using javascript and have it work wherever I load it. The idea is that it does an AJAX-search, so I don't want to code that up every time I put one on the page.

So maybe on pages that I want to put it on that would look like this:

var searchBox = new Search(inputBox);

Ideally, I wouldn't really want to have to link a style sheet everytime I do this. I'm just wondering if performance takes a big hit if I just create tags and add attributes like this:

$('<div></div>').css({
  'background-color': #002323, etc.
});

I feel like its only slightly more verbose, but it will be much easier to manage and use.

Or do you know a better way of doing this?

Maybe this question is brushing the surface of a bigger problem, which is about making CSS object-oriented. I don't want it messing up other things on the page if there are css attributes with the same name. Everything else I do is object-oriented. Are there any CSS solutions or methodologies for this?

+5  A: 

Two things come into mind:

  • If you ever want to change the style, you will have to do it in javascript, possibly at several places.
  • Obviously, applying styles one by one instead of just adding a class is slower.

CSS was designed to make your life easier and honestly I think it wouldn't be very wise to not to use it, unless you have some javascript style framework that does a better job.

DrJokepu
Yes that makes sense - it would be absurd to apply styles like this to a whole document. I'm talking like 3-4 divs that are unique anyways. The other thing is that its not really any more code given the circumstances. I still have to write the CSS, I'd just be writing it in Javascript (jQuery makes it easy!).
Matt
@DrJokepu: "If you ever want to change the style, you will have to do it in javascript, possibly at several places."not necessarily, if you define a global js variable that contains the currently used class, and then assign that variable (rather than a hardcoded class) in your '$("#myelement").addClass(globalClass);' than you're home free. When you want to change it - you only have to change the variable in one place, and then call the function that changes it.I do agree with you, though, that going pure CSS is a lot cleaner in this situation.
yuval
yuval: That's partially what I meant by "some javascript style framework" albeit a rather basic one.
DrJokepu
Your second point is debatable. PPK agrees with you but his test was very 1-dimensional (and for a time flawed in Safari). http://www.schillmania.com/content/entries/2009/yahoo-photos-frontend-thoughts/ points out a better scenario for using `className`s over `style` but observes `style` being faster in general. Another in favor of `style` is Google maps (the most DOM intensive APIs I've ever seen) who use it exclusively. In my experience `style` is consistently fast across browsers. However there is no doubt of the readability/maintainability/semantic enhancement of `className`.
Crescent Fresh
ooh. Thanks for the link crescentfresh. I'll go through that when I get some time - seems real informative though!
Matt
+3  A: 

It seems to me that it rather depends on how much CSS you need to apply to this search component, and whether you need to be able to skin it for different sites. Assuming your javascript is all held in one external file, is it a big problem to create a basic CSS file to go with it, and use the script to dynamically insert a <link> to the CSS file above other <link> elements in the document?

That way you can reuse and skin it very easily, overriding the styles set in the default CSS for any particular site just by adding the appropriate selectors to that site's stylesheet. If you set them all with jQuery, it'll be much harder to change the skin.

Matt Sach
I always wondered if that was kind of hackish or not. That wouldn't be a problem if that was appropriate. Like appending a link to the head.
Matt
I'll admit that when I wrote an autocomplete widget a while back, I just set the requirements for it that both the base .js and .css files should be included in the head, but with enough documentation for someone to reuse and skin it by overwriting the selectors if they wanted. I didn't bother with including the CSS dynamically.
Matt Sach
Yah, I mean I'm not too worried about changing the skin up. This isn't for a bunch of people to download and reuse. I'm just looking for a consistent look that can be "hooked" into pages, that is guaranteed not to interfere with anything else already on the page.
Matt
+1  A: 

The main problems of your search component are obstrusive JS and probably non-accessible tool (except if you took the ARIA road).

The one you're talking about is secondary. You should use carefully named classes, I wonder what can be easier to manage than a class="warning" (background-color: #FE0114; ? no way)

Felipe Alsacreations
Hmm.. maybe my question wasn't clear enough. I'm only making like 4 unique divs and an input. Though under most circumstances you're definitely right- adding classes are easier to manage I feel like in this instances it would be easier to create the element in jquery and just "attach" the attributes to them right then. Its one less link to another file. It's all right there.
Matt