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
Vittorio Somaschini
33,371 PointsA doubt: Ways to Include jQuery in a Project
Hello.
I am doing the jQuery course now and have finished the first stage.
I have actually noted something that is not in the video (or perhaps something that does not work good for myself), here is my doubt:
Best practice as we are told is to put the code to integrate jQuery at the end od the html document. Ok, fine with me, but...
In our case, for example, the alert message displays for a fraction of a second (as the whole document loads), then disappears and appears again (as per jQuery code we have provided).
Isn't is something we should consider not ideal? I do not like it. Not sure if I missed the moment Andrew Chalkley talks about it or if it is not considered or if I have done wrong in my code maybe.
Shouldn't we consider this issue?
Thanks
Vittorio
2 Answers
Stephen Bone
12,359 PointsHi Vittorio
I haven't done this course myself yet so may be jumping the gun but when ever I played around with jQuery/Javascript I normally have it in a separate file. Can't see the harm in it being inside the html though providing its a small amount of code.
I normally wrap my code within the function below as this makes sure the code is only loaded once the html has finished. More info in the link.
$( document ).ready()
http://learn.jquery.com/using-jquery-core/document-ready/
Hope it helps!
Vittorio Somaschini
33,371 PointsOh right.
Still I don't understand why this was completely ignored in the treehouse video.
I mean, in that video it does not even look like the whole thing that I am talking about happens, but when I code it myself via workspace then launch the preview, it goes much slower in a sense; and I can clearly see that happening.
To me it looks like a major thing to discuss.
ty Stephen for your help
Vittorio Somaschini
33,371 PointsVittorio Somaschini
33,371 PointsHi Stephen.
I had a quick look and you are right what you write works fine.
But my question is:
Let's say I want to hide a simple thing, so I compile the needed jQuuery code.
If this jQuery code runs AFTER the html has finished, isn't there a moment (a fraction of a second I would guess) where the user actually sees or can see the thing that I want to hide?
It does not look beautiful to me.
Thanks
Stephen Bone
12,359 PointsStephen Bone
12,359 PointsHi
It could do! If you're talking about an html element however you could always you a CSS style to hide it by default then use jQuery to make it visible when you need to. So for example
If that seems relevant to what you're doing then some further reading linked below:
http://www.w3schools.com/css/css_display_visibility.asp