You can try following options:
LargeFunction.js
function largeFunction(){
// This is a global function and is a part of window object.
// This can be called from anywhere once the file is loaded.
}
index.html
<script src="largeFunction.js" type="text/javascript"></script>
<script>
largeFunction();
</script>
IIFE
These are functions that are executed only once when the file is loaded. They are called the moment they are compiled.
function largeFunction(){
}
(function(){
largeFunction();
})()
You can even try without IIFE
function largeFunction(){
}
largeFunction();
window.onload / $(document).ready()
This events are fired once all elements are rendered. If your JS is manipulating any elements like managing UI states or creating controls like datepickers or searchable dropdowns or adding options to select, these events are preferred.
Note: Both events are not same. You can refer following post window.onload vs $(document).ready()
// Pure JS
function largeFunction(){
}
window.onload = function(){
largeFunction();
}
// jQuery
function largeFunction(){
}
$(document).ready(function(){
largeFunction();
})
Note: Its not a good practice to have large function. You should also try to split function to multiple functions
function getData(callback){
var data = ...
// Gets data from server
data = cleanData(data);
data = processData(data);
if(callback) callback(data); // callback refers to renderUI function
}
function cleanData(data){
// Clean if any field needs formatting
return data;
}
function processData(data){
// You can permute logic to parse data
return data;
}
function renderUI(data){
// Update DOM element with data
}
function largeFunction(){
getData(renderUI);
}
IIFEorwindow.onload. These can be in that file as well.