1

I see a lot of articles in Stackoverflow about including one .js file in another, but I'm no wiser after having read them. Some point to requireJS and some to using jQuery, but it seems the jQuery solution is like the chicken and the egg in that I need to include jQuery in order to use it!!

Thus, let's say I want to include sql-debug.js in my own sql reader .js file:

// my_sql.js reader
var sql = require('../sql/sql-debug.js');
//...

My js code is client code in that it is to be loaded by a browser and code dump all of my js code into the same .html file and use . However, I'd like to place it in appropriately named files.

I read a while back that Javascript is the most used language, and yet the designers never thought of a simple way to include another Javascript file!!

If anyone could provide a working solution to the above simple task it would be much appreciated.

UPDATE

Having looked at the cited answers I still don't get it. Let's say I have the following files all in the same directory: test.html [main html], test_loading.js [copy&paste of code from cited sources] and apple.js [a simple js class] as follows:

test.html

<!DOCTYPE html>

<html>

<head>
    <title>Basic skeleton</title>
    <script type="text/javascript" src="jquery-1.9.0.js"></script>
    <script type="text/javascript" src="test_loading.js"></script>
    <style>
        body{
            /* set margin to 0 and overflow to hidden, to
             use the complete page */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>

<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">

    // once everything is loaded, we run our Three.js stuff.
    $(function () {
          // here we'll put the Three.js stuff
    });

</script>
</body>
</html>

test_loading.js

$.getScript("apple.js", function(){

   alert("Script loaded but not necessarily executed.");

});

apple.js

class Apple {

}

Tell me why this doesn't work.

Graham Seed
  • 742
  • 3
  • 10
  • 24
  • 1
    Lots of information at http://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file might have something you're looking for. – user49438 Feb 16 '17 at 16:34
  • I've added an UPDATE block to illustrate the problem. It's a simple question but not seen as yet a simple well explained answer. – Graham Seed Feb 23 '17 at 11:11
  • 1
    What's not working? Are you seeing any errors in the console? – evolutionxbox Feb 23 '17 at 11:14
  • This doesn't work on browsers. – Stan Feb 23 '17 at 11:54
  • "I've added an UPDATE block to illustrate the problem" — It doesn't illustrate the problem. The code doesn't look like it should output anything at all, and you haven't quoted any error messages, so I don't understand how you can tell what is happening (or what is supposed to happen). – Quentin Feb 23 '17 at 11:55
  • @Stan — You seem to have read the first 1/4 of the question, stopped, and then commented without paying any attention to that later context. (You're wrong anyway, the question mentions RequireJS which supplies a version of `require()` which does work in browsers) – Quentin Feb 23 '17 at 12:00
  • If you load test.html in a browser then you'll see the issues. Many people refer to this and that article but still awaiting a clearly defined answer as to the problem when using jQuery. Stan's answer of "This doesn't work on browsers.", is this correct? My original post stated that I was using a browser. – Graham Seed Feb 24 '17 at 09:45

0 Answers0