0

I am trying to create a button that if I press on it my javascript file gets called. The java script file has many functions so I wasn't sure how to call the functions.

This is how I call the javascript file in my html document.

    <script src="sketch2.js">
</script>

Thank you

3
  • write the function in button click event Commented Jun 4, 2014 at 8:04
  • May be you need to know how javascript events are used. Gve a visit here - w3schools.com/js/js_events.asp. There are lot of libraries out there to do it efficiently, but unless you know what you are doing, they are of no use. Commented Jun 4, 2014 at 8:08
  • Although a Javascript file might contain many functions, it often also has (or should have) documentation that explains which function or functions to call for particular uses. We don't know about your file because we can't see it, you haven't told us about it, and we can't read minds. If you are using a script file you got from another human, ask them. If you are using a library, check the docs. You can try asking again if you can better describe what you are trying to do. Commented Jun 4, 2014 at 8:21

7 Answers 7

2
<script src="sketch2.js">
</script>

this is how you include a javascript file in html. to call all the functions in it, make a function which would call all those functions and call this function on button click.

Then add an inline script containing a Javascript function to call the functions

<script>
function newfunction(){
    fun1();
    fun2();
    ...
}
</script>

Then in the HTML you need

   <button onclick="newfunction()">click here</button>
Sign up to request clarification or add additional context in comments.

Comments

1

Do it like below:

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

1 Comment

Interesting way to load a script. Still, the OP seems to have a file full of functions and seems to say he doesn't really know which functions he wants to call.
1

Create a Button and bind your javascript function to the onclick event. Like this:

<button onclick="functionCall()">
  Do Something
</button>

But you cant just call the whole script file. Ask yourself this question: how should the browser know, which method to call.

In my litte example, it worked:

<html>
<head>
<script>
    function functionCall(){
        alert('hi');
    }
</script>
</head>
<body>
    <button onclick="functionCall()">Do Something</button>
</body>
</html>

1 Comment

Updated my answer and added my test code
1

HTML like this:

<button id="myButton">Click me</button>

Save this in your Javascript file, include the script at the bottom of the html page:

(function () {
    document.getElementById("myButton").onclick(function () {
         // do whatever it is you want to do after the button's been clicked
    });
}());

It shall work regardless of what JS Library you happen to be using (if any)

2 Comments

NB: You don't have to include the function wrapper, but it's good practice to - it prevents any potential conflicts between JavaScript files.
Make sure to use the var unless you want variables to show up in the window object.
0

This can all be simply handled in require.js, but here is a very simple implementation.

<div id="btn">Click to load</div>
var btn = document.getElementById('btn');
var loaded = false;
btn.addEventListener('click', function(e) {
    if(loaded) return;
    loaded = true;
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = 'sketch2.js';
    var first = document.getElementsByTagName('script')[0]; 
    first.parentNode.insertBefore(script, first);

    if(window.addEventListener) {
      script.addEventListener('load', function(e) {
        // now you can access the functions inside the JS file
      });
    } else {
      script.onreadystatechange = function() {
        if(this.readyState === "loaded" || this.readyState === "complete") {
          // access the functions in the JS file
          script.onreadystatechange = null;
        }
      };
    }
});

1 Comment

This code helped me. Thank you :D
0
$(" the id or class of the button ").click(function(e) {

var url = "(your url)sketch2.js";
$.getScript( url, function() {

//function or something..

});

Comments

0

This is you are looking for:

<div class="whatever" onclick="yourfunction()" >Your Button</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.