Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function in one Javascript file from another Javascript file?

I need to call a function in an external ".js" file from another ".js" file, without referencing the external file in the <head> tag.

I know that it is possible to dynamically add an external ".js" file to the which allows access to that file, i can do that like so...

var AppFile = "test/testApp_1.js"; 
var NewScript=document.createElement('script');
var headID = document.getElementsByTagName("head")[0]; 
NewScript.src = AppFile;
headID.appendChild(NewScript);

However...

this is no use to me as the external files need to be stand-alone files that run start-up procedures on...

$(document).ready(function()
{...}

so adding the full file dynamically has an unwanted affect. Also, i cannot pre-reference the external file in the <head> tag as it needs to be dynamic. So, this external file "test/testApp_1.js" contains a function that returns a string variable...

function setAppLogo(){

 var LogoFile = "test/TestApp_1_Logo.png";

 return LogoFile;
}       

I need access to either this function, or I could store the string as a global var in the external file... either way is fine, I just need access to the value in LogoFile without loading the whole external file.

This one has had me stumped for a few hours now so any ideas would be greatly appreciated.

like image 311
user1005240 Avatar asked Jul 03 '12 14:07

user1005240


People also ask

Can I call a JS function from another JS file?

Answer: Use the export and import Statement Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.

How do I call a function from another function in JavaScript?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.

How do you call a function from an external JavaScript file?

Calling a function using external JavaScript file Js) extension. Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.

How can I access data from another JavaScript file?

In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file.


1 Answers

You might benefit from having some sort of app.js file that contains global variables/values that you will want to use from lots of places. You should include this .js file on every page (and maybe minify it/concatenate it with other js if you want to be clever and improve performance). Generally these globals should be attached to some object you create such as var APPNAME = { }; with variables/functions on it that will be used from many places.

Once you have this, then the external '.js' file that you want to load, and the one you are currently in, can both access the global APPNAME variable and all its attributes/functions and use them as desired. This may be a better approach for making your javascript more modular and separatable. Hope this helps.

like image 191
throughnothing Avatar answered Oct 12 '22 12:10

throughnothing