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();
3
  • 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. Commented Aug 12, 2011 at 18:20
  • @coreyward, do you recommend me not having duel scripts? Commented Aug 12, 2011 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? Commented Aug 12, 2011 at 19:22

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Can you elaborate on "using a closure" do you recommend not separating the scripts?
Okay, a lot of things make more sense, I'm still returning a "undefined" for (in your example) utils.
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).
I apologize, I didn't mean "undefined" i meant "not defined" it can't find it.
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.

Comments

0

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

Comments

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.

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.