Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  117] [ 4]  / answers: 1 / hits: 34235  / 11 Years ago, thu, february 28, 2013, 12:00:00

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>

More From » jquery

 Answers
51

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.


[#79931] Wednesday, February 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
malkajillc

Total Points: 652
Total Questions: 107
Total Answers: 98

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
malkajillc questions
;