I have a handful of js scripts in jquery:
vars.js
$(function(){
function yesOrNo(val){
if (val == "yes"){
return "yes";
}
if (val == "no"){
return "no";
}
});
scripts1.js
/// include(vars.js) <-- what would go here?
$(function(){
var var1 = "yes";
var test1 = yesOrNo(var1);
alert(test1);
});
scripts2.js
/// include(vars.js) <-- what would go here?
$(function(){
var var2 = "no";
var test2 = yesOrNo(var2);
alert(test2);
});
scripts3.js
/// include(vars.js) <-- what would go here?
$(function(){
var var3 = "yes";
var test3 = yesOrNo(var3);
alert(test3);
});
How can i call the function yesOrNo from a different scripts.js page?
If you want to add another file add type="module" parameters in your main code, then add the line import {yesOrNo} from './vars.js'; in your code.
Then, in the vars.js file, add the function as export function yerOrNo, not function yesOrNo.
My edit of your code:
vars.js
export function yesOrNo(val){
if (val == "yes"){
return "yes";
}
if (val == "no"){
return "no";
}
}
main.js
import {yesOrNo} from './vars.js'
$(function(){
var var2 = "no";
var test2 = yesOrNo(var2);
alert(test2);
});
On your html call main.js like that
<script src="main.js" type="module"></script>