Import and Export does not even work with type="module"

Asked

Viewed 54 times

-2

I have a Javascript file called "redirect.js", in which it has only two similar functions, which serve only to redirect to a specific page, "redirect.js", containing the following code:

export function redirectToCorrectPage() {
  window.location.href = "right-answer.html";
}

export function redirectToIncorrectPage() {
  window.location.href = "wrong-answer.html";
}

There are other 3 Scripts that I am trying to import these functions, but without success. 'redirect' script is in the same folder as these. So I’m trying to import it this way:

import { redirectToCorrectPage, redirectToIncorrectPage } from './redirect.js'

I’m having two kinds of mistakes:

  1. javascript - "Uncaught Syntaxerror: Cannot use import statement Outside a module"
  2. Uncaught Syntaxerror: Unexpected token '{'

On my HTML pages, which are directing to these scripts, all are with " script type='module' "

Anyway, my question is: What is the best way to import these functions, and why the way I did it is not working.

Thank you for your attention.

1 answer

-3


When you want to use different functions of Avascripts files in html, you need to import them with priority care

For example:

redirect.js

 const redirectToCorrectPage = () => {
  window.location.href = "right-answer.html";
}

const function redirectToIncorrectPage = () => {
  window.location.href = "wrong-answer.html";
}

index.html

<html>
   <body>
      <script src="redirect.js"></script>
      <script>
          redirectToCorrectPage()
      </script>
   </body>
</html>

Import and export are to be used in nodejs projects as modules or requires

Browser other questions tagged

You are not signed in. Login or sign up in order to post.