I must have all my "scripts calls" within a SINGLE "$(document).ready()" . Why I do this :
<head>
...
</head>
<body>
...
<script src="http://....jquery call"></script>
<script>
$(document).ready(function(){
<?php include('script_01.js'); ?>
<?php include('folder/script_02.js'); ?>
<?php include('../../folder/script_03.js'); ?>
});
</script>
</body>
This works perfectly in all browsers, but : Do I have the right to do that ? Is it compatible W3C ? Thanks. Nico.
It should work and you do have the right to do that, they are your scripts ;)
But it's not optimal, the best way would be to gather the contents of all files in a self executing function, then minimizing that code and serving a single file to the user.
Like so:
(function(){
// contents of all javascript files
})();
Than run that through a minimizer like UglifyJS, maybe via something like this.
The nice thing is that all your global variables are not in the window
scope. So say you have a variable page
, without the self executing function you can access it from the console, alter it etc. But if you wrap it in a self executing function, the scope changes from the window scope to the 'private' scope of the self exc. function. This way the user cannot change the variables your script (might be) using.