0

I have a project I'm working on that has multiple pages. Both of them have a JavaScript tag. I want to be able to get a variable from the other one.

First HTML

<html>
   <head></head>
   <body>
      <script>
          var example = "stringText"
      </script>
   </body>
</html>

Second HTML

<html>
   <head></head>
   <body>
      <script>
          console.log(example)
      </script>
   </body>
</html>

I have tried making a .js file and saving a variable there and accessing it from the other file but it didn't work, the variable would clear itself when it got to the other page.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Every new page resets all javascript variables, and is generated brand new. If you want to store a variable across pages, you need to use Cookies. – Jack Cole May 20 '20 at 20:51
  • 2
    Why do you need multiple html plages? What are you trying to acomplish? This doesn't look like the right approach to most situations. – junvar May 20 '20 at 20:53
  • junvar I need different code for each page, it's a video viewing website. I would be the admin and I could add videos to the site and people are able to view it. It's only something I'm doing for practice, I'm only 15. Someone answered my question and deleted it. I'm using localStorage to solve my problem. – Nermin Sabic May 20 '20 at 21:01
  • Thank you! I will try that too! – Nermin Sabic May 20 '20 at 21:11

1 Answers1

0

It is a possible way to approach.

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
Uday
  • 464
  • 3
  • 11
  • That was the first thing I had tried. But the variables would reset when I switched pages. Maybe I had made a mistake. I'll give it another try later. Thank you. – Nermin Sabic May 24 '20 at 21:20