Eliminate render-blocking Javascript and css in above-the-fold content
Your page has 7 blocking scripts and 10 css resources. This causes delay in rendering your page.
google suggests to defer or asynchronously load blocking resources
So i decided to move all the js/jquery files in the footer.
But i am not able to move “jquery.min” file to footer.
Actually, it should be the other way round, if at all. :-/ Where exactly did you put your scripts? They should be right before the closing </body> tag. Do you have a live example? Or maybe there are some hints in the console (such as “failed to load resource” or “$ is not defined”)?
It must not be in a footer. You’ll have to put it right before the closing </body> tag.
This would certainly help to find the error. It doesn’t matter if it’s long, as code content will get a scroll bar at a certain length (just be sure to actually mark it as code here!).
“async” + $(document).ready() would defeat the whole purpose of asynchronously loading the script; and “defer” is not needed if you put the scripts at the end of the body as explained above. Also, browser support could be an issue.
External script called in the <head> loads in before the page renders.
Moving the call before the </body> does let the page render sooner.
But if subsequent code that relies on that library being available runs before it loads in you’ll have problems.
I guess it would be possible to wrap those functions with some kind of timer that checks for “if jQuery” but IMHO it would be easier to put the library in the <head> and ignore what “Google says” unless you’ve noticed a significant problem caused by it.
But why would that happen without loading it asynchronously? Er, @vinpkl do you maybe have inline JS somewhere in your markup?
I think the most efficient way would be to concatenate your scripts beforehand, or even better, bundle it with Browserify. This way you’ll only have one single HTTP request for your JS, and module dependencies are handled automatically so that nothing can go wrong there.
I just can’t imagine it’s a good idea to asynchronously load a script other scripts are relying on. You’ll have no control whatsoever whether jQuery is loaded and evaluated in time or not… it may work for you now, but you just can’t be sure. For example, this:
will likely fail (it does on my machine/connection).
Anyway, if the your scripts are working in the head but not at the bottom of the body (having them in the same order, of course), the only reason why it might fail I can think of is inline-scripting. We’d really need to see your markup.
you were right. it doesnt works everytime. fails sometimes.
i have modified your example code snippet to show how my code is setup and why it doesnt work when i put or load “jquery.min” at the bottom or before end of .
Even though all my code is covered with
$document.ready function but still it doesnt work ?? dont know why ??
Aha! :-) You’re loading jQuery after you’re attempting to use it. Normally (i.e. w/o async), scripts are getting loaded and evaluated strictly one after the other; by the time you’re calling $ the browser doesn’t know anything of jQuery yet, hence the error. Just reverse the scripts and you’ll be fine. (I’m still not sure why it worked when they were all in the head, though…)
But it is a requirement for any scripts attached in the head as otherwise they block the loading of the page - that’s the main reason why almost all scripts need to go just before the </body> tag
In a very small (private) project I might just load them separately before the </body> tag in appropriate order. But as your applications grows, you might want to structure the components of your code in separate files, such as one for each model, view etc. This approach quickly becomes hard to maintain as you’d have to keep your <script> tags in sync, as well as inefficient as it would mean a lot of server requests.
Instead, it’s common practice to concatenate (and while at it, minify) all your components beforehand and load them all at once in one single <script> tag. If you have a lot of dependencies (such as a controller requiring a certain model to work on), it might make even more sense to design your components as modules, require() them where needed and bundle your code with a tool such as Browserify. This would look like e.g.
var $ = require('jquery');
$( //... jQuery operations
Browserify will then take care that everything is there when it’s needed, and again you’ll only have a single file to load.
If all my code is right before then closing </body> tag, there won’t be any code near the opening tag. ;-)
Yes, like
if (typeof window.jQuery === 'function') { // ...
or just
if (window.jQuery) { // ...
But this is usually not necessary if your code is getting loaded properly. You might do such a check if you’re writing, say, a library that is meant to be included in others’ projects.
I don’t really see the relation here… but as I mentioned earlier in this thread, you rarely need to check this anyway. Just load jQuery before your other code at the end of the body and you’re fine. :-)
You don’t need document.ready - with the scripts just before the </body> tag the document is already ready before the scripts start to run.
Also using jQuery from one of the common depositories (as you are doing) 99% of the time it will already be downloaded before your visitor even selects to visit your page.