0

As you can see, I am trying to use the variable counter from a function from one file in a function of another file. I defined counter in the function without beginning it with "var" which I think should make it a global variable. However, it seems it doesn't work. SOS!

//function used in symptoms.html
  function checkboxes(){
    counter = parseInt(document.querySelectorAll('input[type="checkbox"]:checked').length); //counter would be a global var?
    if (counter>=10){
          location.href='end.html';
    }else{
      location.href='state.html';
    }
    }

//function used in another file
    function statedegree(x){
        var conclusion=x*counter;//uses counter var from checkboxes()
         if (conclusion>=18){
           location.href= 'end.html';
         } else{
           location.href='end1.html';
         }
    }
  • I'd recommend using script files instead of inline scripts – PotatoParser Aug 21 '20 at 20:35
  • @PotatoParser Thank you for the suggestion, but I did put in its own js file – Adelene Guo Aug 21 '20 at 20:37
  • In that case, you should declare `counter` as a global variable, however, if you are looking to access variables from different pages, then I suggest looking into the [localStorage API](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) – PotatoParser Aug 21 '20 at 20:38
  • 1
    Few things you can do to debug: make sure the second file is linked after the scripts in the html file, console.log the counter to check its value, make sure statedegree is called after checkboxes is called. – Rod911 Aug 21 '20 at 20:48

2 Answers2

1

Persistence across different pages can be done using the localStorage API:

function checkboxes(){
    localStorage.setItem('counter', document.querySelectorAll('input[type="checkbox"]:checked').length);
    if (counter>=10){
          location.href='end.html';
    }else{
      location.href='state.html';
    }
}
function statedegree(x){
    var conclusion=x * parseInt(localStorage.getItem('counter') || 0);
     if (conclusion>=18){
       location.href= 'end.html';
     } else{
       location.href='end1.html';
     }
}
``
PotatoParser
  • 1,008
  • 7
  • 19
0

I don't know the order of your file, but the another file must have to be included in the html after the first which contains the declaration of the variable. In the same way, I think that your function which declare the variable must be called before the second.

Otherwise, you can use window.counter to declare your variable as global across multiple files.

Please correct me if I'm wrong

ozob
  • 36
  • 1
  • 5
  • Yes, the checkboxes function is called before the second. However, window.counter (I believe) is used to get the url or the page name of an element. To declare a global variable, I tried to name it without the "var" part in the function which should've made a global variable, but it didn't for some reason. – Adelene Guo Aug 21 '20 at 21:10
  • I think you can find what you need here https://stackoverflow.com/a/5786899/12207529 I hope it will help you – ozob Aug 22 '20 at 02:32