0

I am writing a javascript app and implementing three.js. To use this I use the script tag in my html file:

<script src="./scene.js" type="module"></script>

I have my main index.js file that contains all of the parameters and functions for my main index.html page. In this index.js I have an array of objects which I would like to pass to scene.js

I do not know how to achieve this. My understanding is that to export functions/parameters, the js file needs to be a module (declared in html using type="module"). If this is not declared as a module, I get the error 'unexpected token 'export''

If I make this a module though, I cannot reference functions in my html file that are triggered by buttons.

My index.html has my two scripts defined as this:

<script src="./index" ></script>
<script src="./scene.js" type="module"></script>

My index.js I want to export one array:

export var myArray = []

My three.js file contains the following first few lines:

import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r127/build/three.module.js';

import { myArray } from './index.js';

I am confused how to continue. I cannot have everything in the one js file, as I could not import my three module

samp17
  • 547
  • 1
  • 4
  • 16
  • What do you mean by "*functions in my html file that are triggered by buttons*"? – Bergi May 01 '21 at 20:20
  • in my html I will have: The showAddForm() is a function in my index.js. If the script is referenced using type="module" then I get the error that showAddForm() could not be found – samp17 May 01 '21 at 20:38
  • 1
    [Don't put event handlers in html attributes](https://stackoverflow.com/q/6941483/1048572), install them using DOM methods instead :-) But if you still want to do that, just install the function in the global scope from the module, by assigning `window.showAddForm = function(e) { … }` – Bergi May 01 '21 at 20:40
  • 1
    I think this is the clarification I was looking for, thank you. So I am back to having one JS file with everything in it, and in my script tag in the html file I declare it using type=“module”. Then any functions I want to trigger by html button I attach using the DOM addEventHandler method. If my understanding is correct, this is the answer. – samp17 May 02 '21 at 07:20

0 Answers0