Calling functions from js file to html page

html, javascript, jquery

Solution

Yes. If you move the contents of the script tag to a file with the path "js/main.js" and then added a script

<script src="js/main.js"></script>

after the other scripts, it will be able to call the functions. Including an external script is equivalent to having the text from that script inline in the file.

Scripts can read the contents of previous scripts so having multiple scripts on the page is similar to concatenating them all into a single file, which means that if you add a script below the other scripts it will be able to "see" everything in the others

With regard to questions about rloader

rloader does lazy loading to pull in scripts when you need them.

For more on lazy loading And you can learn about rloader from its site (I'm no expert on that)

For what its worth I would not recommend using rloader if you really only have 4 scripts on one page. Its overkill. If you're planning on having a much bigger project, then you can use it or the more popular requirejs to manage your scripts across pages.

Problem

I have three js files for all of my webpages, and I have pre-defined sets of functions to call for each web page. Could I move all of these functions to a new js file which would make then calls to other functions in a different js file? I read about rloader at http://code.google.com/p/rloader/, but I am not sure if I could use it. ``` <script src="js/rootNameSpace.js"></script> <script src="js/jquery-min.js"></script> <script src="js/ui.js"></script> <script src="js/form.js"></script> <script type="text/javascript"> console.dir(com); com.rela.form.helloWorld1(); com.rela.form.helloWorld2(); </script> ```

Original source

Related problems