Search code examples
javascriptjqueryhtmlgetscript

jQuery getScript vs Embedding Script in File


I was wondering whether use jquery´s getScript or not.

I have a jQuery plugin that I need to include in my site, but I am unsure what to do and I am wondering if there is a difference between these approaches:

In my case, I have to include a rather big script, but only for IE browsers. At this moment I do it like this:

if($.browser.msie){
 //minifed code
}

Now I was wondering if this:

if($.browser.msie){
 $.getScript('link to minified script');
}

would have any advantages/disadvantages? Or if it is more or less the same thing?


Solution

  • There will be disadvantages to getting an external script.

    i.e. ONE HTTP request

    if($.browser.msie){
     //minifed code
    }
    

    TWO HTTP requests

     if($.browser.msie){
     $.getScript('link to minified script');
    }
    

    Your browser can only generally cope with 2 http requests at any one time. So from a performance point of view, the first will be faster.

    further explanation

    with getScript, IE users will have a smaller request, (-the size of your "minified code"). But everyone else will have to download the external script everytime (+ the size of your external script * number of page requests) + delay in response due to extra HTTP request.

    with the minified code everyone will have to download your size of your minifed code (+minified code size).

    Presuming your minifed code is say 5K and is accessed 10 time that means:

    IE GetScript:

    (5K * 10) + performance of using second HTTP request = 50K+

    Non-IE

    0

    ////////////////////////////////////////////////////////

    IE nonGetScript

    5K

    non-IE

    5K