質問

I have a javascript source code file which is about 32kb, and has javascript code arranged in modules. I was wondering if it would increase page load time if i put each module into a separate file as managing the current code of over 2000 lines is quite tedious and some times confusing

役に立ちましたか?

解決

Short answer, yes and no. If you have a <script> tag pointing to each module file, then yes it will slow down the page due to the increased number of network calls to those files. The real answer is that's the way you should CODE it, but that's not the way you should serve it up to your user.

I recommend a tool called Minify, which will combine and minify all your JS files so you only need one <script src="/min/f=mod1.js,mod2.js,mod3.js" type="text/javascript"></script> tag

You can also manage "groups" of files, which are simply PHP arrays with an associative key you reference in the script src. For example:

'module_js' => array(
            '//js/module1.js',
            '//js/module2.js',
            '//js/module3.js'
            )

Then your script tag would look like:

<script src="/min/g=module_js" type="text/javascript"></script>

After Minify comines and minifies your code, it will cache it on your server so subsequent requests will hit the cache file so the PHP won't need to process.

http://code.google.com/p/minify/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top