<html> <script> //some largeFunction() //load a script dynamically based on the previous code document.write("<script src='//...'><\/script>"); </script> </html>
Question: is it possible to move the largeFunction()
out of the static html
page and put it into a js
file? If yes, how could I then call that function statically before writing the <script>
tag?
Calling a function using external JavaScript file Js) extension. Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.
Step 1: Firstly, we have to type the script tag between the starting and closing of <head> tag just after the title tag. And then, type the JavaScript function. Step 2: After then, we have to call the javaScript function in the Html code for displaying the information or data on the web page.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute.
Short answer: Yes.
As long as you load the first script containing the function first, you can call the function anywhere, as long as it's loaded first.
<script src="file1.js" type="text/javascript"></script> <script src="file2.js" type="text/javascript"></script>
In this example, make sure file1.js
contains your largeFunction()
function. You can then call largeFunction();
inside file2.js
.
You can also do this:
<script src="file1.js" type="text/javascript"></script> <script> largeFunction(); </script>
Just make sure your FIRST script contains the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With