|
|
||
|
When using JavaScript you have the choice whether to include the script directly into your HTML file or to put it into a separate file with the ".js" extension. The primary advantage of using a separate file for your JavaScript is that you may reuse the same script in any of your HTML files. How to call an external JavaScriptJavaScript tutorial on calling an external JavaScriptThe following is an example of a webpage calling an external JavaScript file named "JSname.js". In this example, notice that a function from the external JavaScript file is accessed by a button in the same way you would access a script that is directly inside your HTML file. The following code example is what you could put in the "JSname.js" file. Notice that only JavaScript is included in this file. Do not attempt to put SCRIPT tags in your ".js" file as the SCRIPT tag is already included in the HTML file. If you put the HTML file above in the same folder as the ".js" file above. You will have a webpage with a button that calls the JavaScript which then opens a message box displaying "Hello". This JavaScript tutorial example on how to call an external JavaScript file is very basic, however it is a very good example on how to call an external JavaScript script from an HTML file. |
||