How to import js file dynamically

  • 2020-03-30 01:22:06
  • OfStack

When we want to dynamically import a js file, we can use the following method
 
var docFunc = { 

 

loaddefJquery : function() { 
//alert(ucapSession.hostPath + ucapSession.appPath); 

//Ucapsession.apppath is the root directory
docFunc.load_script(ucapSession.appPath 
+ "ftdxxglxt/js/jquery-1.8.1.min.js"); 
docFunc.load_script(ucapSession.appPath 
+ "ftdxxglxt/js/jSuggest.js"); 
}, 

 
load_script:function(url){ 
var x = new ActiveXObject("Msxml2.XMLHTTP"); 
x.open('GET', url, false); 
x.send(''); 
eval(x.responseText); 
var s = x.responseText.split(/n/); 
var r = /^(?:function|var)s*([a-zA-Z_]+)/i; 
for (var i = 0; i < s.length; i++) 
{ 
var m = r.exec(s[i]); 
if (m != null) 
{ 
window[m[1]] = eval(m[1]); 
} 
} 
} 

} 

Call method in HTML page or JSP page:
 
<script> 
docFunc.loaddefJquery(); 
</script> 

Related articles: