javascript - Executing separate .js files - Stack Overflow

I am trying to learn how different .js files canbe included in a HTML file, so that my code bees more

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?

Share Improve this question edited May 23, 2017 at 10:25 CommunityBot 11 silver badge asked Jan 27, 2014 at 13:40 user227666user227666 7844 gold badges18 silver badges34 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

Your 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744978896a4604316.html

相关推荐

  • javascript - Executing separate .js files - Stack Overflow

    I am trying to learn how different .js files canbe included in a HTML file, so that my code bees more

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信