我想将由另一个 JavaScript 文件( imageUploadDisplay.js
)填充的数组( imageArray[]
—)导入到另一个 JavaScript 文件函数( postInfoAndImages.js
)中。下面是我尝试使用的 imageUploadDisplay.js
函数。
// In my imageUploadDisplay.js
var imageList = [];
function myImageList() {
return imageList;
}
正如其他人所解释的,我使用以下 jQuery 将 imageUploadDisplay.js
导入 JavaScript postInfoAndImages.js
:
// In my postInfoAndImages.js
var imageList = [];
$.getScript('imageUploadDisplay.js', function () {
// Script is now loaded and executed.
alert("Script loaded, but not necessarily executed.");
imageList = myImageList(); // Picture array
console(imageList);
});
原文由 Thabiso Prosper Ramokopu 发布,翻译遵循 CC BY-SA 4.0 许可协议
您甚至不需要在第一个文件中包含第二个文件即可使用该功能。如果第一个文件在您的代码中先于第二个文件被调用,您可以只引用该函数。
在 postInfoAndImages.js 中,只需调用有问题的函数: