1

I want to organize my JavaScript so I thought I would make a functions JS file. Is there anyway I can call the functions from functions.js from global.js?


EDIT

functions.js:

var get_selects;
get_selects = {
    getLanguages: function() {
    }
}

global.js:

get_selects.getLangueges();
Phil
  • 10,948
  • 17
  • 69
  • 101
  • If the functions from `functions.js` are global functions, you can call them from `global.js` but make sure that you include the scripts in the correct order: 1. functions.js, 2. global.js. However, I recommend you to **not** define global functions - that just pollutes the global namespace (unnecessarily). A better alternative would be to have one global object - like `PHIL` - and then define those functions as members of that object. – Šime Vidas Aug 12 '11 at 18:20
  • @coreyward, do you recommend me not having duel scripts? – Phil Aug 12 '11 at 19:00
  • haha, I will remember this though! If I were to surround all my javascript inbetween `$(document)ready()` tags would that effect anything with DOM readiness? – Phil Aug 12 '11 at 19:22

5 Answers5

5

Yes, functions defined at the top level are automatically available in the global scope (window in a browser), and this is typically not desirable.

Another approach that would mitigate this is to group your functions into a single object so you aren't polluting the global scope with a whole bunch of unrelated functions.

var utils;
utils = {
  toast: function(message) {
    alert("Notification: " + message);
  },

  sum: function(a, b){ return a + b; }
}

utils.toast('Email sent');
utils.sum(1, 2);
coreyward
  • 77,547
  • 20
  • 137
  • 166
  • Can you elaborate on "using a closure" do you recommend not separating the scripts? – Phil Aug 12 '11 at 18:25
  • Okay, a lot of things make more sense, I'm still returning a "undefined" for (in your example) utils. – Phil Aug 12 '11 at 19:05
  • Whoops! I had left a stray closing parenthesis in there. I added code for the `its_a_trap` function too so that it's valid (and more fun). – coreyward Aug 12 '11 at 19:11
  • I apologize, I didn't mean "undefined" i meant "not defined" it can't find it. – Phil Aug 12 '11 at 19:19
0

If both scripts have been included in the same HTML file, sure, it will work out of the box. Best way to know is to try it.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
0

All .js files load top level functions into the global namespace. So, yes.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

simply call it like anyother functions

yourFunctionName(yourFunctionParams);

be aware, you need to include your functions.js BEFORE your global.js, else it won't see your functions.

Charles Forest
  • 1,035
  • 1
  • 12
  • 30
0

From the moment you include a JS in the HTML file, all the functions become available. So, if you make like this, it will work:

<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript" src="global.js"></script>

But (as soon as I know), you must include "functions.js" first. Otherwise, "global.js" will not be able to find the calls. You can also make a little function inside "global.js" to include "functions.js" on the fly, like this:

function include(js_path){
    //By Fabrício Magri e Micox
    //http://elmicox.blogspot.com/2006/12/include-em-javascript.html
    var new= document.createElement('script');
    new.setAttribute('type', 'text/javascript');
    new.setAttribute('src', js_path);
    document.getElementsByTagName('head')[0].appendChild(new);
}

Than, on the beginning of your "global.js" you call this function to include the contents of "functions.js" on the section as soon as the browser requests "global.js"