0

I want to use the doAlert function in the index.js file. Now, how do I use import and export in this situation?

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

//index.js
/*'index.js' is an empty file.*/

//doAlert.js
function doAlert() {
    alert('Hello');
}

1 Answer 1

1
/* index.html */
<script type="module" src="index.js"></script>
<script type="module" src="doAlert.js"></script>

/* doAlert.js */
export function doAlert() {
    alert('Hello');
}

/* index.js */
import { doAlert } from './doAlert.js';
// use it
doAlert();

More info on JS modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Sign up to request clarification or add additional context in comments.

12 Comments

Yes. I tried it. But, it is not working. It was supposed to show an alert on the screen.But it doesn't.
the code is working in the link you provided. But it is not doing same in my code editor. I'm using VS Code. Is there something wrong with my editor? By the way, my index.html, index.js and doAlert.js files are in the same folder. The link you provided , there's another src folder.
Sorry, am I understand correctly, that you want to see alert in your text editor? It won't work because there is no any engine there to interpret and run your code.
This is clear to me now. Appreciate the help. Arigato...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.