Skip to content
Advertisement

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
JavaScript
  • script1.js
JavaScript
  • page2.html
JavaScript
  • script2.js
JavaScript

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?

Advertisement

Answer

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

script1.js

JavaScript

then in script2.js

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement