3

I want to import an array (imageArray[]) that is being populated by another JavaScript file (imageUploadDisplay.js) into another JavaScript file function (postInfoAndImages.js). Below is my imageUploadDisplay.js function that I tried to use.

// In my imageUploadDisplay.js var imageList = []; function myImageList() { return imageList; } 

As explained by others I use the following jQuery to import imageUploadDisplay.js into the 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); }); 
5
  • You define the same variable in two files?CommentedAug 6, 2019 at 18:57
  • Yes was attempting to at least copy the values from 1 array to the otherCommentedAug 6, 2019 at 19:00
  • It's not clear what problem you're having.CommentedAug 6, 2019 at 19:00
  • I want to access the array created and populated by imageUploadDisplay.js into a function created in postInfoAndImages.jsCommentedAug 6, 2019 at 19:04
  • You are not importing anything into postInfoAndImages.js, contrary to what you are thinking. You are simply executing a piece of code which is injecting another script to the document itself, and not to postInfoAndImages.js, thus it should already be globally accessed, depending on what's inside the injected script.
    – vsync
    CommentedAug 6, 2019 at 19:09

3 Answers 3

6

For modern browsers like Firefox, Chrome, Safari and Opera, just use ES6 modules.

In your imageUploadDisplay.js create a named export:

// imageUploadDisplay.js var imageList = []; export function myImageList() { return imageList; } 

Then just import the function:

// then in postInfoAndImages.js import { myImageList } from './path/to/imageList.js'; var imageList = myImageList(); 

In your HTML, you no longer need to include imageUploadDisplay.js (this is done by the import at runtime).

Instead, include the importing script with type="module":

<script type="module" src="./path/to/postInfoAndImages.js"></script> 
6
  • I got an 'Uncaught SyntexError: Unexpected tokenCommentedAug 6, 2019 at 19:18
  • That happens if you forget type="module".
    – connexo
    CommentedAug 6, 2019 at 19:19
  • Okay the error is gone but there's no feedback on my console.log(imageList) and my function doesn't execute past itCommentedAug 6, 2019 at 19:36
  • What do you mean there's no feedback on my console.log(imageList) and my function doesn't execute past it?
    – connexo
    CommentedAug 6, 2019 at 19:55
  • I've put the initial imageList[] in my imageUploadDisplay.js in a console log and it shows me the number of files in it so now that I've imported it in postInfoAndImages.js the imageList[] doesn't show anything when put it in the console logCommentedAug 6, 2019 at 20:07
3

You don't even need to include the second file in your first to use the function. If the first file is called in your code before the second, you can just refer to the function.

<script type="text/javascript" src="imageUploadDisplay.js"/> <script type="text/javascript" src="postInfoAndImages.js"/> 

In postInfoAndImages.js, simply call the function in question:

var imageList[]; imageList = myImageList(); 
2
  • logically that makes sense but I got a' UncaughtReferenceError: myImageList is undefined'CommentedAug 6, 2019 at 19:48
  • It should work if you have it referenced properly. I did a quick test. In my first js I have: var cars = ["Saab", "Acura", "Volvo"]; function myCarList() { return cars; } A console.log(myCarsList()) returns the above array. In my second file I called it again but with a blank array and I get the blank array returned. cars = myCarList(); console.log(cars);
    – elkefreed
    CommentedAug 6, 2019 at 20:12
3

You need to work with exports and imports.

// File imageUploadDisplay.js let myImageList = () => { return imageList; } export default imageList 

And then import it into the other file

// postInfoAndImages.js import imageList from "./imageUploadDisplay" // -> Do something with imageList 
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.