admin管理员组文章数量:1289384
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 /, 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();
.rela.form.helloWorld1();
.rela.form.helloWorld2();
</script>
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./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();
.rela.form.helloWorld1();
.rela.form.helloWorld2();
</script>
Share
Improve this question
edited Apr 20, 2013 at 11:03
asked Feb 28, 2013 at 14:11
user2008608user2008608
2
- Of course, the browser doesnt care that they are seperate files or not – DavidB Commented Feb 28, 2013 at 14:12
- If you include a script then you can call any public function in it from the next line onwards, whether it's in the html file or another included script file. Including the script is equivalent to copying and pasting it. – Reinstate Monica Cellio Commented Feb 28, 2013 at 14:14
3 Answers
Reset to default 1Yes. 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 remend 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.
If you have dynamic generated pages you can have different names/actions/controllers whatever. Then you can
echo '<script type="text/javascript">$(document).ready(function(){'.$page_name.'();});</script>';
Then you can declare global functions in any JS file, yes you can have any number of JS files, and splited in any way you want, they are all global.
function name1(){...};
If you have a big application with many JS files you can split then into more files, in a single folder, then add a minify plugin to "collect" them in a single output file (or a JS builder).
rloader is a dynamic loading script, basically Injects JS files in your document (http://ntt/2008/02/10/4-ways-to-dynamically-load-external-javascriptwith-source.html). I don't remend using it, except if you have a very big application and use a MVC http://coding.smashingmagazine./2012/07/27/journey-through-the-javascript-mvc-jungle/ that loads only the current module.
It is always better to put code in separate files (as far as they are less in size and count). This will allow to be cached by browser $(document).ready will keep you safe for other dom elements that are not loaded.
Create something like this:
<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 src="js/pages/some-page.js"></script>
some-page.js
$(document).ready(function(){
console.dir();
//call to function form.helloWorld1
.relais.form.helloWorld1();
.relais.form.helloWorld2();
});
A better option would be bine files (If they are mon on each page). rootNameSpace.js, jquery-min.js, ui.js, form.js into a file say mon.js. You can use Google Closure to do that.
<script src="js/mon.js"></script>
<script src="js/pages/some-page.js"></script>
本文标签: javascriptCalling functions from js file to html pageStack Overflow
版权声明:本文标题:javascript - Calling functions from js file to html page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741445959a2379201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论