Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    DMN
    DMN is offline
    New Coder
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Calling a Function in External JS File

    Hi All, I'm very new to Javascript and would like some pointers. If I have multiple functions in the same external Javascript file, how would I call a particular function inside that external JS file from a web page. For instance suppose I have a JS file called myJSFunction.js and within this file I have three functions like the following:
    Function1()
    {
    Do Something
    }

    Function2()
    {
    Do What Ever
    }

    Function3()
    {
    Do Stuff
    }

    How would I call Function2() from a web page? Any suggestion is greatly appreciated, thanks in advance.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Question

    Have you tried:

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

    Some place before the functions are called, but after elements of page have been loaded.
    Usually just before the ending </body> tag.

  • #3
    DMN
    DMN is offline
    New Coder
    Join Date
    Dec 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi, thanks for replying. I know about linking to an external JS file as you have mentioned above, but what I would like to know is after placing a link to the external JS file in the script element, can I then call Function1, Function2, or Function3 in the body of my page like the following:
    Code:
    <body>
          <div>
             <a href="javascript:Function1()">Click Here </a><br>
             <a href="javascript:Function2()">Click Here </a><br>
             <a href="javascript:Function3()">Click Here </a>
          </div>
    </body>
    Last edited by DMN; 03-16-2013 at 05:24 AM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,589
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Never attach JavaScript insiide the href attribute.

    Best is to not jumble any of the JavaScript in the HTML at all and put ALL the JavaScript in the external file.

    <a id="a1" href="nojs.htm">click here</a>

    and in the external HTML

    document.getElementById('a1').onclick = function() {..... ; return false;}

    the code for your function goes in place of the ..... in the above command that is in the external JavaScript file.

    You attach JavaScript in the external file to HTML in the web page using the id of the element you want to attach it to (or in some cases class names if the same code is to be attached to multiple elements

    The code suggested by DMN was considered to be bad practice in Netscape 2 and worse practice in every browser since then.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by DMN View Post
    How would I call Function2() from a web page?
    You call (execute) the function when an event happens. For example:

    Code:
    <input type = "text" id = "mytext" onblur = "function1()">
    <input type = "button" value = "Click Me" onclick = "function2()">
    <img src="logo1.jpg" onclick="function3()">
    <select id = "myselect" onchange = "function4()">
    <img src="smiley.gif" alt="Smiley"  onmouseover="function5(this)" >
    Is this what you mean?

    I like fish and chips, but you can't get that there [Paris] - all you can get are snails' legs. - Commentator Talksport
    Last edited by Philip M; 03-16-2013 at 09:43 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •