Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
How can I use a variable from another JavaScript file?

I have two html pages, each with a js file.

When I click on a button on page 1, it should change a variable param to true and redirect the user to page 2.

In page 2 I have to check if param = true and then continue my script.

  • page1.html
<div class="container-fluid" id="about">
  <div class="row">
    <div id="mainsubtitle">Page 1</div>
    <button id="edit" type="button">Go to page 2</button>
  </div>
</div>
  • script1.js
var param = false;

edit.addEventListener("click", switchToOpenSession);
...
function switchToOpenSession() {
    param = true;
    ...
}
  • page2.html
<div class="container-fluid">
  <div class="row">
    <div>Page 2</div>
    <button id="create" type="button">Create account</button>
  </div>
</div>
  • script2.js
if (param)
   /* do this */

I tried export/import param but I got Unexpected token 'export' error. I tried type="module" but it doesn't work. The samples that I found was two js files in the same html page!

Can someone help please?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you cannot access variable from different page, except you load page2.html using ajax. Alternative way using cookie or localStorage

script1.js

var param = false;

edit.addEventListener("click", switchToOpenSession);
...
function switchToOpenSession() {
    param = true;
    localStorage.setItem('param', true)
    ...
}

then in script2.js

if (localStorage.getItem('param') == 'true') // note the quotes
about 4 years ago · Juan Pablo Isaza Relatório

0

you can export the variable from first file

var first;

export { first }

and in your second file you can import that variable

import { first} from './first.js'

OR

<script type="module" src='first.js'></script>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't share variable states like that. These two pages are totally different instances, so the context is not the same.

Option 1. When you redirect from one page to another you can add URL param and parse it on page 2.

Option 2. Use localStorage. I assume you have the same origin while talking on page 1 and page 2 so the stored data will be visible.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda