use*_*240 7 javascript jquery file external dynamic
我需要从另一个".js"文件中调用外部".js"文件中的函数,而不引用标记中的外部文件<head>.
我知道可以动态添加一个外部".js"文件到允许访问该文件,我可以这样做......
var AppFile = "test/testApp_1.js";
var NewScript=document.createElement('script');
var headID = document.getElementsByTagName("head")[0];
NewScript.src = AppFile;
headID.appendChild(NewScript);
Run Code Online (Sandbox Code Playgroud)
然而...
这对我没用,因为外部文件需要是运行启动程序的独立文件...
$(document).ready(function()
{...}
Run Code Online (Sandbox Code Playgroud)
因此动态添加完整文件会产生不良影响.此外,我不能预先引用<head>标签中的外部文件,因为它需要是动态的.所以,这个外部文件"test/testApp_1.js"包含一个返回字符串变量的函数...
function setAppLogo(){
var LogoFile = "test/TestApp_1_Logo.png";
return LogoFile;
}
Run Code Online (Sandbox Code Playgroud)
我需要访问这个函数,或者我可以将字符串存储为外部文件中的全局变量...两种方式都可以,我只需要访问值LogoFile而不加载整个外部文件.
这个让我难倒了几个小时,所以任何想法都会非常感激.
您希望在 jQuery 使用 ajax 加载后加载该文件,然后在成功的 ajax 函数中运行相关脚本。
请参阅 jQuery 的 getScript 函数:http://api.jquery.com/jQuery.getScript/
$(document).ready(function(){
$.getScript("http://domain.com/ajax/test.js", function(data, textStatus, jqxhr) {
console.log(data); //data returned
console.log(textStatus); //success
console.log(jqxhr.status); //200
console.log('Load was performed.');
//run your second script executable code here
});
});
Run Code Online (Sandbox Code Playgroud)