0

I've use YSlow to check the load-speed of my web. According to the rule, it is better to have one big external javascript rather than many external javascripts. It is written in the rule that using a lot of external javascript will cause a lot of http request which is not good.

I'm using CKEditor, and I want to combine it with other javascripts into a big external javascript. But, everytime I load it, there is always errors ocured. After read the code, I think I know why is it happens.

if ( CKEDITOR.loader )
CKEDITOR.loader.load( 'core/ckeditor' );
else
{
    // Set the script name to be loaded by the loader.
    CKEDITOR._autoLoad = 'core/ckeditor';

    // Include the loader script.
    if ( document.body && (!document.readyState || document.readyState == 'complete') )
    {
        var script = document.createElement( 'script' );
        script.type = 'text/javascript';
        script.src = CKEDITOR.getUrl( '_source/core/loader.js' );
        document.body.appendChild( script );
    }
    else
    {
        document.write(
        '<script type="text/javascript" src="' + CKEDITOR.getUrl( '_source/core/loader.js' ) + '"></script>' );
    }
 }

CK Editor seems to load other external javascript which has location relative to it. Moving the script into other file will surely cause an error.

So, my question is: Is there any general work-around for this? I mean, it might be other scripts with such a behavior (not only CK-Editor).

EDIT: I think using head.js (http://headjs.com/) is the best options here

7
  • One large file isn't always best - read this: headjs.com/#theory Commented Aug 31, 2012 at 0:52
  • 1
    Once the JS files are properly cached, and assuming they're placed at the bottom of the body as they should, end-users won't see any significant change in load speed for 1 vs 3-4 scripts - modern browsers can load scripts in parallel and will only fetch a 304 response from the server. Commented Aug 31, 2012 at 0:53
  • @SamDufel: Thanks for the link. I never know that before Commented Aug 31, 2012 at 1:00
  • @FabrícioMatté: So YSlow metric is not relevant today? Commented Aug 31, 2012 at 1:00
  • Not saying whether is relevant or not, but try putting yourself at your visitors' place -- disable caching and Ctrl+F5 your page on multiple browsers, then check if the loading time actually improves after joining all your JS files. Obviously, for the first load, minified files do have a visible advantage (if your JS files are HUGE), but after the first load it isn't much relevant -- unless of course the user has completely disabled caching, but then it's a rare corner-case and s/he is either used to slow page loading times or has a fast enough connection that makes loading time irrelevant. Commented Aug 31, 2012 at 1:44

2 Answers 2

1

When trying to load all JavaScripts at once dynamically usually you'll encounter dependancy issues or library conflict issues.

Examples: Dependancy Issues - Requiring a file that needs jQuery before jQuery has been loaded. Conflict Issues - When working on large scale products you may end up encountering conflicting libraries. For instance loading two files with the same library name but different functionality.

If you're truly seeing a lot of slow down and want to combine all of your JS files set dependencies in your file combination code. A simplified example of this is creating an array of libraries that are loaded prior to any main JS pieces.

Hope this helps in resolving your issue!

Sign up to request clarification or add additional context in comments.

1 Comment

that is exactly what happens to me. But I've make a library for it, so that it will only make the cache in the very first time. Head.js is a good solution so far
0

Well, I don't know which server language you're using but, in case it's PHP, I suggest you to try Smartoptimizer for your concatenation/minification issues.

Using it, you just add your js as you would and it takes care of processing them. Nowadays, I'm using it with YepNope and I believe it would work with HeadJS too.

1 Comment

Let me try it. I don't sure that any library can deal with ck-editor case. As you can see in my explanation, this is likely not a usual case. Thank you for point out YepNope !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.