0

I have a web structure like this:

  • Head scripts (like Normalizer)
  • Body starts
  • HTML and Script containing jQuery 1
  • HTML and Script containing jQuery 2
  • HTML and Script containing jQuery 3
  • Body ends
  • jQuery loads here
  • Other scripts (like G. Analytics)

The problem is that the HTML and Scripts contain jQuery code but jQuery is loaded last. How do I do to execute succesfully these Scripts once that jQuery has been loaded?

Please note the following restrictions:

  1. I cant load jQuery before the HTML and Scripts
  2. The jQuery Code needs to be where it is (Quick explanation: I will be loaded from an external source that changes often)

I tried putting the scripts on the function window.onload function but it does not work...

Any solutions?

adelriosantiago
  • 7,762
  • 7
  • 38
  • 71

2 Answers2

1

requirejs is your answer.

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.

http://requirejs.org/docs/jquery.html

Aliendroid
  • 515
  • 3
  • 9
0

try something like this

<html>
    <head>

        <script>
            function page_init(){
                //your javascript code with jquery
            }
        </script>
    </head>
    <body>
        <!-- html stuff goes here -->
        <script src="jquery.js"></script>
        <script>
            $(function(){
                page_init();
            });
        </script>
    </body>
</html>
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
  • In general always give an explanation on why code given in an answer would work for the benefit of OP and future users with similar issues. This code may work well if the script using jQuery is defined in the HTML document. What about external files which use jQuery which are loaded separately? Given the limitation on how many files can be downloaded at a time in a browser and each file might take less or more time to download than others how does this answer address that issue? – Nope May 13 '14 at 07:55
  • Thanks @rajesh, I can see what the point of your solution however it does not comply with the expected structure. The HTML and jQuery code must be close each other because are closely related and are loaded from a user-editable, external source. With this structure I would need to "replace" 2 parts of the document every time. – adelriosantiago May 13 '14 at 08:02