54

I am looking to add a class to the body element of the DOM. For something so simple, and with the body element itself loading quick (at least, I would think it would load quicker than, say, an element buried deem in the DOM), must I really wait for the jQuery Ready event to do such a simple task? I'm looking to avoid a "flicker" effect when adding the style to the body, since I'll have different CSS styles attached to this class take effect when added.

I can do something like:

 jQuery(window.document).ready(function () {
 jQuery("body").addClass("home");
 });

But is there a faster, yet safe way? I don't care if its jQuery or native JavaScript

asked Jul 3, 2013 at 20:31
4
  • 2
    I am not sure what you mean by 'safe' - that seems perfectly safe and fast (how fast depends on the amount of content). What I usually do, to avoid a flicker effect, is hide the body by CSS and show it by jQuery after all the scripts have run. It does slip up from time to time, for example where an element is sized based on the size of another element, but those scripts just need to go after the line which shows the body. Commented Jul 3, 2013 at 20:35
  • safest and fastest way would be to go in the HTML and add the class to the <body> element Commented Jul 3, 2013 at 20:43
  • surely the fastest and safest way is to put it in the body tag ?! Commented Jul 3, 2013 at 20:53
  • @Alnitak true, but I'm using a ASP.NET master page and I only want this class to be added when rendering the home page. Commented Jul 3, 2013 at 21:47

2 Answers 2

132
document.body.className += ' home';

Performance comparision : className vs classList vs addClass :

Update(based on PSL's comment)

or for newer ones document.body.classList.add("home");

Make sure you do this under the <body>, it won't work if applied from a <head> script

answered Jul 3, 2013 at 20:35
Sign up to request clarification or add additional context in comments.

8 Comments

or for newer ones document.body.classList.add("home");
make sure it's inside <body>
@PSL: Did you really try to compare those??? That test (assigning a handler vs executing a jQuery snippet) makes absolutely no sense, it proves nothing.
Why not document.body.className += ' home';? Am I missing something in regards to the extra plus sign?
@Pete: No, but I vote to leave it like this to accentuate the need of the space. But that is just my opinion. Also you suggested too many spaces.
|
3

Right after the opening body tag, you can create a script tag :

<body>
<script>
 $('body').addClass('home')
</script>
<!-- HTML content bellow -->
</body>

The only condition is that the jQuery is loaded in the head.

answered Jul 3, 2013 at 20:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.