简体   繁体   English

如何在其他JavaScript中访问变量?

[英]How to access variable in other javascript?

Suppose Now I've a HTML file with 假设现在我有一个HTML文件,

<script type="text/javascript" src="js/first.js"></script>
<script type="text/javascript" src="js/second.js"></script>

Both JS contain sets of functions, how can I access second.js from first.js? 两个JS都包含函数集,如何从first.js访问second.js?

<script> elements are executed in order. <script>元素按顺序执行。 You can't access things defined in second.js while first.js is being first executed, but if you define a function in first.js and set it up so that it's called after second.js has been loaded it will see anything defined in second.js. 首次执行first.js时,您无法访问second.js中定义的内容,但如果您在first.js中定义一个函数并进行设置,以便在加载second.js之后调用该函数,它将看到定义的任何内容在second.js中。 A DOM ready or a "load" event might be a start. DOM准备就绪或“加载”事件可能是一个开始。

May be you can act like this. 也许你可以这样。 Make it in Order. 使其井然有序。

<script type="text/javascript" src="js/first.js"></script>
<script type="text/javascript" src="js/second.js"></script>
<script>
    //call the function at first.js which will access second.js
    exampleFirstJSFunc();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM