admin管理员组文章数量:1406926
I am trying to learn how different .js
files can be included in a HTML
file, so that my code bees more modular. I am following this page Can we call the function written in one JavaScript in another JS file?. I am confused at one point, that if I include files like this:
<script language="javascript" src="a.js">
<script language="javascript" src="b.js">
If a.js
contains:
function alertOne() {
alert("one");
}
and b.js
contains:
function alertTwo() {
alert("two");
}
then do I need to separately call the functions in HTML file or just including the files like <script language="javascript" src="b.js">
this, would execute alertTwo()
function?
I am trying to learn how different .js
files can be included in a HTML
file, so that my code bees more modular. I am following this page Can we call the function written in one JavaScript in another JS file?. I am confused at one point, that if I include files like this:
<script language="javascript" src="a.js">
<script language="javascript" src="b.js">
If a.js
contains:
function alertOne() {
alert("one");
}
and b.js
contains:
function alertTwo() {
alert("two");
}
then do I need to separately call the functions in HTML file or just including the files like <script language="javascript" src="b.js">
this, would execute alertTwo()
function?
- 2 A function is executed only when you call it. – MrUpsidown Commented Jan 27, 2014 at 13:42
- Including a Javascript file is essentially the same as just including the Javascript straight into the HTML, the function will not auto execute once included. – Nunners Commented Jan 27, 2014 at 13:43
- By the way. Did you try it by yourself? This is quite easy to test, IMO. – MrUpsidown Commented Jan 27, 2014 at 13:47
- Yes, I tried it myself and I couldn't understand the point, see my ment in Anthony's answer – user227666 Commented Jan 27, 2014 at 13:48
3 Answers
Reset to default 3Your JavaScript files are just declaring the functions. If you want them to actually execute then yes, you'd need to call them at some point. Whether that's inside another .js file, inside a <script>
tag in your HTML page, or as part of an event handler on an element, is up to you and depends on what exactly you want.
The code
function alertTwo() {
alert("two");
}
declares a function that can be called later
The code:
function alertTwo() {
alert("two");
}
alertTwo();
declares a function, and then immediately calls it.
The code:
(function alertTwo() {
alert("two");
})();
declares a function that immediately calls itself.
It doesn't matter if your code is directly in the HTML file, or included via script
tag. Nothing is "executed" or done special via script
tag. It's effectively like saying "paste the stuff from that file here."
The function alertOne()
in a.js
can be called from b.js
if both a.js
and b.js
are included in your HTML.
本文标签: javascriptExecuting separate js filesStack Overflow
版权声明:本文标题:javascript - Executing separate .js files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744978895a2635689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论