How does this code run?

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”>
$(‘html’).addClass(‘js-on’);
</script>
<style type=“text/css”>
html.js-on #method1 {color:red;}
</style>
<p id=“method1”>Method #1</p>

I’ll just ask. What’s the fasted out of these five methods if I need/want to place the needed code at the bottom of the page? Unfortunately I don’t know enough about js to answer that myself. Reason being I have a large photo gallery that needs to load and I don’t want any extra markup slowing it down. http://www.visibilityinherit.com/code/add-css-with-javascript.php

I suggest then that you create a demo page for each of the six techniques, so that we can then help you come up with some timings for each of those techniques and also figure out certain pros and cons of each technique that you may want to stay aware of.

Well I set up some test pages. But each time I run them it is a different number. So that’s no help it seems. You honestly don’t know what the js engine does in that circumstance?

Well I set up some test pages. But each time I run them it is a different number. So that’s no help it seems.[/QUOTE]

Most web browsers have a performance timeline where you can determine how long things take. There are also ways to have JavaScript record how long it takes so that you can perform multiple tests and then take an average.

Where do you have those test pages?

There is no single JS engine. We browsers all have their own different engines with different performance metrics, so the microseconds that one technique might have over another aren’t as important as the time it takes for you to manage and maintain and understand whichever technique that you end up deciding to go with.