在.js文件中使用外部javascript文件 [英] Using external javascript files in a .js file
问题描述
我想在另一个javascript文件中使用外部javascript文件。例如,我可以将所有全局变量存储在globals.js文件中,然后从网站逻辑logic.js调用。然后在index.html中,我会插入标记。如何在logic.js中使用globals.js?
I would like to use an external javascript file in another javascript file. For example, I could store all my global variables in a globals.js file and then call then from the website logic logic.js. Then in the index.html, i would insert the tag. How do I use the globals.js inside the logic.js?
推荐答案
Javascript没有任何隐含的包含此其他文件机制,如css的@include。您只需要在标记中的逻辑文件之前列出您的全局文件:
Javascript doesn't have any implicit "include this other file" mechanisms, like css's @include. You just have to list your globals file before the logic file in the tags:
<script type="text/javascript" src="globals.js" />
<script type="text/javascript" src="logic.js" />
如果在逻辑文件中的任何内容启动之前保证全局变量可用,则可以做得很慢轮询循环,以便在调用init()或任何函数之前查看某个特定变量是否可用。
If guaranteeing that the globals are available before anything in the logic file fires up, you can do a slow polling loop to see if some particular variable is available before calling an init() or whatever function.
这篇关于在.js文件中使用外部javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!