Simple and fast question:
I need the jquery library for a site, so I access it in my header.php file as such:
<head>
<!-- include jQuery library -->
<script type="text/javascript" src="../javascript/jquery.js"></script>
</head>
Now my question is should I also be putting some other php scripting in there to make sure it is only included when I need it for a given page? My gut tells me I am absolutely right to only load the jquery files when I need them, yet I swear I always see things like this loaded without thought to if they are actually needed for the given situation. So perhaps there is no harm in always having it there.
So should it be like this?? Pseudo code:
<head>
<?php
//set jquery variable to true or false in configure.php file based on situation
if($jquery){
//include the jquery script!!
}
else{
//jquery??? we don't need that here
}
?>
</head>
It’s generally better to load only what you need for a page, but if you do include jQuery everywhere, make sure:
1) It’s minified (to reduce overhead).
2) You serve from a CDN (e.g. Google’s). The likelihood of the user having a cached version (and thus a faster page load) will be much higher. See:
http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/