Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why my js code runs first even if i place it at the bottom of the page???

I place my js code just before the </body> tag but it still runs before my page. Please help me.

Show your code please.

Should be correct. make sure your saving your file before running it again.

We will be able to help more if you can show us your code.

How to get a image with the code ?

I just typed "<script>...code...</script>" just before the </body> tag and it runs before the html

2 Answers

Try this

<script>
window.onload = function(){
    // put your code inside of here
}
</script>

Like Arturo stated, use window.onload or if using jquery:

$(document).ready(function() {
    // jQuery code goes here
});

You wrote a commentm as if jQuery is not a JavaScript any more :smile:

jQuery is just a library which already has some functionality built on JavaScript, so that you can write native JavaScript code inside jQuery callbacks