61

I would like to call a javascript function in an external JS file, using the onClick function on a button in this file, form.tmpl.htm.

  <button type="button" value="Submit" onClick="Call the external js file" >

The javascript file is in Public/Scripts/filename.js. I have a template file in template/form.tmpl.html. The root folder contains the Public and template folders.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
verdure
  • 3,201
  • 6
  • 26
  • 27
  • 7
    You can't "call a javascript file". If the script file is already loaded, you can "call a javascript function in that file". Or, if the script file is not yet loaded, you can "load the script file" or "load the script file and call a function in it". Which of these is what you want to do? – jfriend00 Oct 17 '11 at 04:56
  • 2
    "Call the external js file" does'nt mean anything you can call a function in a JS file but not call a file. –  Oct 17 '11 at 04:56
  • Technically speaking, loading a JavaScript file into the dom that contains some simple code or self-executing function is functionally equivalent to calling a function directly. It's more efficient to just load a function once and call it on an as-needed basis. Adding JS function calls to inline HTML breaks the separation between content and state. All JS should be kept within JS files. – zzzzBov Oct 17 '11 at 05:55
  • @jfriend00 - I would like to call a javascript function in that file. – verdure Oct 18 '11 at 01:52
  • 2
    If the javascript file that contains the function is already loaded, then just call the javascript function directly. All javascript functions at the top level are available everywhere once the JS file is loaded. If the function is called `foo`, then just call it with `foo()`. – jfriend00 Oct 18 '11 at 01:59

6 Answers6

55

If you want your button to call the routine you have written in filename.js you have to edit filename.js so that the code you want to run is the body of a function. For you can call a function, not a source file. (A source file has no entry point)

If the current content of your filename.js is:

alert('Hello world');

you have to change it to:

function functionName(){
 alert('Hello world');
}

Then you have to load filename.js in the header of your html page by the line:

<head>
 <script type="text/javascript" src="Public/Scripts/filename.js"></script>
</head>

so that you can call the function contained in filename.js by your button:

<button onclick="functionName()">Call the function</button>

I have made a little working example. A simple HTML page asks the user to input her name, and when she clicks the button, the function inside Public/Scripts/filename.js is called passing the inserted string as a parameter so that a popup says "Hello, <insertedName>!".

Here is the calling HTML page:

<html>

 <head>
  <script type="text/javascript" src="Public/Scripts/filename.js"></script>
 </head>

 <body>
  What's your name? <input  id="insertedName" />
  <button onclick="functionName(insertedName.value)">Say hello</button>
 </body>

</html>

And here is Public/Scripts/filename.js

function functionName( s ){
 alert('Hello, ' + s + '!');
}
Fry Simpson
  • 1,125
  • 11
  • 15
  • 3
    Sorry I think this is rather confusing. The code does not run as expected, use https://jsfiddle.net/ or something when there are multiple files. Furthermore I believe it does not answer the answer. – partizanos Aug 27 '15 at 09:29
  • 2
    This helped me. I was able to call the function after loading it within the body so it didn't have to be loaded in the head after all. – mmurrietta Mar 02 '18 at 21:52
  • 1
    Thanks, this is what I wanted to do. Call an external .js file from a driver Onclick line in the body html. A little roundabout but it works. – renaissanceMan Feb 21 '20 at 23:44
54

I have to agree with the comments above, that you can't call a file, but you could load a JS file like this, I'm unsure if it answers your question but it may help... oh and I've used a link instead of a button in my example...

<a href='linkhref.html' id='mylink'>click me</a>

<script type="text/javascript">

var myLink = document.getElementById('mylink');

myLink.onclick = function(){

    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "Public/Scripts/filename.js."; 
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;

}


</script>
Mike Sav
  • 14,805
  • 31
  • 98
  • 143
  • 2
    Where do you call the function that's in the js file? – HiTech Aug 11 '16 at 18:25
  • 2
    Where? Do you mean how? If the script is loaded you can call a function within the loaded script as normal. Perhaps you want to ask a further question? – Mike Sav Aug 15 '16 at 06:53
  • 1
    Ignore my question. I didn't realize this was being called in the markup. Thank you for the reply! – HiTech Aug 15 '16 at 12:51
10

By loading the .js file first and then calling the function via onclick, there's less coding and it's fairly obvious what's going on. We'll call the JS file zipcodehelp.js.

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Button to call JS function.</title>
</head>
<body>
    <h1>Use Button to execute function in '.js' file.</h1>
    <script type="text/javascript" src="zipcodehelp.js"></script>
    <button onclick="ZipcodeHelp();">Get Zip Help!</button>
</body>
</html>

And the contents of zipcodehelp.js is :

function ZipcodeHelp() {
  alert("If Zipcode is missing in list at left, do: \n\n\
    1. Enter any zipcode and click Create Client. \n\
    2. Goto Zipcodes and create new zip code. \n\
    3. Edit this new client from the client list.\n\
    4. Select the new zipcode." );
}

Hope that helps! Cheers!

–Ken

Kenneth Wagner
  • 111
  • 1
  • 6
6

It is totally possible, i did something similar based on the example of Mike Sav. That's the html page and ther shoul be an external test.js file in the same folder

example.html:

<html>
<button type="button" value="Submit" onclick="myclick()" >
Click here~!
<div id='mylink'></div>
</button>
<script type="text/javascript">
function myclick(){
    var myLink = document.getElementById('mylink');
    myLink.onclick = function(){
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.src = "./test.js"; 
            document.getElementsByTagName("head")[0].appendChild(script);
            return false;
    }
    document.getElementById('mylink').click();
}
</script>
</html>

test.js:

alert('hello world')
partizanos
  • 1,064
  • 12
  • 22
2

You could simply do the following.

Let's say you have the JavaScript file named myscript.js in your root folder. Add the reference to your javascript source file in your head tag of your html file.

<script src="~/myscript.js"></script>

JS file: (myscript.js)

function awesomeClick(){
  alert('awesome click triggered');
}

HTML

<button type="button" id="jstrigger" onclick="javascript:awesomeClick();">Submit</button>
ShellZero
  • 4,415
  • 12
  • 38
  • 56
0

You can load all your scripts in the head tag, and whatever your script is doing in function braces. But make sure you change the scope of the variables if you are using those variables outside the script.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93