How to link external javascript file onclick of button

前端 未结 8 1176
旧时难觅i
旧时难觅i 2020-11-29 20:22

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

I wo

8条回答
  •  猫巷女王i
    2020-11-29 20:58

    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:

    
    	
    

    so that you can call the function contained in filename.js by your 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, !".

    Here is the calling HTML page:

    
    
    	
    		
    	
    
    	
    		What's your name? 
    		
    	
    
    

    And here is Public/Scripts/filename.js

    function functionName( s ){
    	alert('Hello, ' + s + '!');
    }

提交回复
热议问题