def load_javascript_in_order(script_list, callback=None, suffix=''):
    """
    Returns the HTML code to load a list of javascript files and ensure its execution order.

    A callback function to be executed after all the scripts in the list can be passed as the name of a existing javascript function.
    """
    load_functions_js = u''
    for i in range(len(script_list)):
        id, src = script_list[i]
        try:
            next_id = script_list[i + 1][0]
        except:
            next_id = None
        if next_id:
            next_function = u'load_%s%s' % (next_id, suffix)
            next_else_function_js = u'%s();' % next_function
            next_function_js = (
                u"""
                add_onload(script_%(id)s, %(next_function)s);
            """
                 % {'next_function': next_function, 'id': id})
        elif callback:
            next_else_function_js = u'%s%s();' % (callback, suffix)
            next_function_js = (
                u"""
                add_onload(script_%(id)s, %(callback)s%(suffix)s);
            """
                 % {'callback': callback, 'id': id, 'suffix': suffix})
        else:
            next_else_function_js = u''
            next_function_js = u''
        load_functions_js += (
            u"""
        function load_%(id)s%(suffix)s() {
            if (!document.getElementById('%(id)s%(suffix)s')) {
                var script_%(id)s = document.createElement('script');
                script_%(id)s.id = '%(id)s%(suffix)s';
                script_%(id)s.src = '%(src)s';
                script_%(id)s.type = 'text/javascript';
                document.getElementsByTagName('head')[0].appendChild(script_%(id)s);
                %(next_function)s
        """
             % {'id': id, 'src': src, 'next_function': next_function_js,
            'suffix': suffix})
        load_functions_js += (
            u"""
            }
            else {
                %s
            }
        }
        """
             % next_else_function_js)
    first_function = """
        function add_onload(element, func) {
                var oldonload = element.onload;
                var oldstc = element.onreadystatechange;
                if (oldonload != 'function') {
                    element.onload = func;
                }
                else {
                    element.onload = function() {
                        oldonload();
                        func();
                    }
                }
                if (oldstc != 'function') {
                    element.onreadystatechange = func;
                }
                else {
                    element.onreadystatechange = function() {
                        oldstch();
                        func();
                    }
                }
        }
    """
    try:
        first_function += u'load_%s%s();' % (script_list[0][0], suffix)
    except:
        pass
    js = (
        u"""
    <script type="text/javascript">
        %s
        %s
    </script>
    """
         % (load_functions_js, first_function))
    return js
