[SOLVED]
I use to build pages normally without javascript and only then to add javascipt features. This way, I can set all divs with tools that require javascript with display:none and then in the first line on javascript to turn it visible:
<script type="text/javascript">
$(document).ready(function(){
$('#jsdivs').show();
});
</script>
Doing so I get out of the way of people who for any reason have their browsers javascript disabled.
Following this spirit, I'd like to find a way to not load any javascript by default and then, if javascript is enabled have instructions to load the javascript files.
Right now I am loading JQuery like this (for cache reasons, many people may already have this cached):
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
And another file with all the other javascript minimized. All in the end, just before the HTML body tag.
I saw many opinions here and here but none that help me with this. At first, I'd like to know if that's a good way of getting this done. Thanks a lot.
UPDATE:
@Lèse majesté indicated me an old discussion saying that browsers do not load javascript files when it is disabled. As I didn't think the information was enough conclusive, I made some tests disabling javascript in Firefox via the plugin "Web Developer" and YSlow (another FF plugin for Firebug) keeps on showing all javascript information, not mentioning that "Live Headers" (another fantastic FF plugin that shows the headers in realtime) also showed that a _utmc cookie (by the Google Analytics script) was present in the request.
@Marcel Korpel made a very good (though apparentely snob) comment, which opened the way for a real solution. he said that Google Analytics uses a "method to dynamically include ga.js. The closing tag (and immediately opening tag) is needed to let the browser update the DOM tree and actually load and execute the added script."
With the information on his indication I could come up with this PHP code (I had to put the PHP code in Pastie.org because Stackoverflow was messing with it):
That prints:
<script language="javascript" type="text/javascript">
document.write('\<script src\="js\/minified\.js" type\="text\/javascript"\>\<\/script\>');
</script>
This time YSlow and Live Headers say nothing about any javascript file.
- I have just found out this: http://code.google.com/speed/page-speed/docs/rtt.html#AvoidDocumentWrite