Skip to main content
1-Visitor
November 5, 2021
Solved

Add js file into home.js

  • November 5, 2021
  • 1 reply
  • 2901 views

I am currently using the method :

$scope.app.fn.loadResourceScript("app/resources/Uploaded/btnMult.js"); //On community was told that is a method to import modules from Uploaded.

 

then ,to connect the modulo function to the $scope i did:

$scope.mult=function(x){

mult(x)
}

 

the file "btnMult.js" ,contains :

function mult(x){

return x*x

}

 

There is nothing more on the file, but when i run the file ,i got an error ,telling "mult is not defined" 

 

Can somebody give me a help? I did every option that are related on importing but nothing works...

 

Best answer by RolandRaytchev

Hi @RB_10104920 ,

I never used the mentioned api but I loaded scripts fromt the Upload project folder (or subfolders ) using following scripts:

 //---------------Loading the API----------------------- 
$scope.asyncLoadScript = function (fname) {
 return new Promise((resolve, reject) => {
 if(fname) { var head = document.head || document.getElementsByTagName('head')[0],
 script = document.createElement('script');
 script.async = true; script.onload = resolve;
 script.onerror = reject; script.type = 'text/javascript';
 script.src=fname; head.appendChild(script); } });}

var mathe =undefined;
//////////////////////////////////////////
$scope.mathModuleLoad = function() {
console.log("mathModuleLoad started"); 
var mathe =undefined;
$scope.asyncLoadScript("app/resources/Uploaded/MyMathFunc.js").then(
 function() { console.log("myMathFunc was loaded successfully") ;TESTLOAD();}, 
 function() { console.log("error for when loading myMathFunc.js module");} );
}
//
$scope.mathModuleLoad(); //LOAD dhere the myMathFunc.js for

The example code above loaded e.g. my math js module

Here another version which will load both js or css files from project folder:

$scope.loadjscssfile= function(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
 var fileref=document.createElement('script')
 fileref.setAttribute("type","text/javascript")
 fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
 var fileref=document.createElement("link")
 fileref.setAttribute("rel", "stylesheet")
 fileref.setAttribute("type", "text/css")
 fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
 document.getElementsByTagName("head")[0].appendChild(fileref)
}
 
//loadjscssfile("app/resources/Uploaded/myscript.js", "js") //dynamically load and add this .js file
//$scope.loadjscssfile("app/resources/Uploaded/style1.css", "css")

 

1 reply

21-Topaz I
November 8, 2021

Hi @RB_10104920 ,

I never used the mentioned api but I loaded scripts fromt the Upload project folder (or subfolders ) using following scripts:

 //---------------Loading the API----------------------- 
$scope.asyncLoadScript = function (fname) {
 return new Promise((resolve, reject) => {
 if(fname) { var head = document.head || document.getElementsByTagName('head')[0],
 script = document.createElement('script');
 script.async = true; script.onload = resolve;
 script.onerror = reject; script.type = 'text/javascript';
 script.src=fname; head.appendChild(script); } });}

var mathe =undefined;
//////////////////////////////////////////
$scope.mathModuleLoad = function() {
console.log("mathModuleLoad started"); 
var mathe =undefined;
$scope.asyncLoadScript("app/resources/Uploaded/MyMathFunc.js").then(
 function() { console.log("myMathFunc was loaded successfully") ;TESTLOAD();}, 
 function() { console.log("error for when loading myMathFunc.js module");} );
}
//
$scope.mathModuleLoad(); //LOAD dhere the myMathFunc.js for

The example code above loaded e.g. my math js module

Here another version which will load both js or css files from project folder:

$scope.loadjscssfile= function(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
 var fileref=document.createElement('script')
 fileref.setAttribute("type","text/javascript")
 fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
 var fileref=document.createElement("link")
 fileref.setAttribute("rel", "stylesheet")
 fileref.setAttribute("type", "text/css")
 fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
 document.getElementsByTagName("head")[0].appendChild(fileref)
}
 
//loadjscssfile("app/resources/Uploaded/myscript.js", "js") //dynamically load and add this .js file
//$scope.loadjscssfile("app/resources/Uploaded/style1.css", "css")

 

1-Visitor
November 11, 2021

Ty Roland =D  worked