Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

How does this code run?

2:24 am on Oct 25, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 23, 2007
votes: 0

How does this code run? This is using jQuery to use JS to run CSS. For times when JS is off. Works perfect. It adds the class .js-on to the html if js in on. My question is this? I want to place all this code at the bottom of the page. I was surprised it works this way. Does the browser have to travel all the way back up to find the html tag (thus slowing it down?) or does it work different. I have 5 different ways to do this. I'm looking for the fastest. This way is clean because I already have jquery library on the page. Thanks.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
<style type="text/css">
html.js-on #method1 {color:red;}

<p id="method1">Method #1</p>
7:52 pm on Oct 25, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
votes: 22

Don't use jQuery or any other frameworks, they're not real code, they'll lead you as far away from writing real code and you'll end up having no comprehension over what you're trying to do. Figure out how the DOM works in conjunction with JavaScript.

function setstylebyid(i,p,v) {var n = document.getElementById(i); if (n != null) {n.style[p] = v;}}

- John
1:58 am on Oct 28, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
votes: 0

Keep the style declaration in the head section.

If there is existing style declaration add line "html.js-on #method1 {color:red;}" to it will suffice.

I already have jquery library on the page

Then do no include include it a second time with
<script type="text/javascript" src="jquery.js"></script>

When a statement like:
<script type="text/javascript" src="jquery.js"></script>
is included in head section, page load is delayed until jquery.js is loaded.

To reduce delay it can be placed at end of body. If you do that, script like $('html').addClass('js-on'); will need to be moved also as it relies on jquery.js being loaded.

So if you decide to put script in body, you will need to consider all script on page, and possibly move it all.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members