Skip to content Skip to sidebar Skip to footer

Create And Call External Js Files

I've spent a great deal of time creating and testing an application with quite a few functions. Now that I have configured everything and it works how I want, I would like to begin

Solution 1:

To link an external javascript file, place a script tag with a src attribute pointing to your file.

<scriptsrc="legend.js"></script><scriptsrc="layers.js"></script><scriptsrc="init.js"></script>

Calling a function requires nothing special.

onclick="myFunction();"

Solution 2:

Could you not just put all of those functions in one .js file and then call the file from within your web page?

<scriptsrc="../scripts/javascript.js"></script>

And then call on your functions as and when you need them?

Solution 3:

The first thing you might consider doing do is to create a non-global "namespace" for your functions in a fashion such as the following:

window.MyApp = window.MyApp || {};

The above line can be at the top of every file; the first time it is invoked it creates a new namespace/object, subsequently it returns the one you previously created.

Then you can move your functions under MyApp in a manner such as the following:

MyApp.func1 = function() {...}

Google for creating Javascript namespaces, and possibly also the Javascript module pattern

Post a Comment for "Create And Call External Js Files"