Use global variable in more than one js file

Asked

Viewed 5,742 times

6

I have the file Geragriddados.js and in this file I created a global variable and a function.

nomeTela = "";

function redirecionaTela() {
    //redireciona para tela que chamou a tela de Dados.
    location.href = nomeTela + ".html";
};

I would like to call this variable nomeTela in another JS document Geragraficoacucar.js and feed it with a value. And when I call the function redirecting() of Geragriddados.js in another HTML screen that this value is still fed.

Arquivo Geragraficoacucar.js:

function redirecionaTelaDados()
{
    nomeTela = "acucar";
    location.href = "dadosGrafico.html";
}

In the two HTML files of mine I have the link to the file . js, as below:

<script src="scripts/GeraGridDados.js"></script>

But this way I did, the global variable is empty when I call in the one that will use this nameTell.

Is this shape I’m making right? Is there a better way to do this?

  • 1

    Even though this is not your first question, welcome to Stackoverflow in Portuguese. I made an issue in your question removing thanks and greeting, given that our communication is not instant. I suggest you take a read on that topic in meta where this subject (greetings and thanks) has been dealt with. If there is anything else we can help, ask at the goal.

  • @emanuelsn thank you, I will follow this suggested pattern.

  • Your question may contain 3 different answers, according to the point of view of understanding, we can state that: "Você quer saber como utilizar uma variável global em mais de um arquivo js" this is already a natural behavior of javascript, and also you want to "Utilizar uma variavel que não se perca ao redirecionar para outra página"(only in this case the reply of @Antony Alkmim would be correct) But reading the contents of your question, you may not need to do this, (according to my reply), then please check what you really want before you mark.

  • What I needed is what was suggested by Antony Alkmim and also by you Paulo Roberto. All the answers were helpful, thank you.

2 answers

11


If redirecting you lose the information contained in the variable because the file GeraGridDados.js will charge again.

An easy solution to this is to use localStorage

// Armazenar
localStorage.setItem("nomeTela", "Joao");
// Obter
nomeTela = localStorage.getItem("nomeTela");
//Limpar
localStorage.clear();

There are other ways, with sessionStorage, where the data is stored only during the session, ie when close the browser the data is lost.

Take a look at documentation localstorage.

  • It worked!!! Just one more doubt, at some point that localStorage("nameTela") can be deleted, or it will be deleted by the process itself?

  • I edited the answer with the method to clean!

  • Just one thing: this your answer is correct, but it is correct in parts, because reading the OP question we can get 3 different answers.

2

In fact, reading the title of your question, you say how to use global variable in more than one js file, Well, to answer that, I can tell you that regardless of how many files. js you have included in the page, all the code will be compiled as a whole and any variable that is declared in this way: variavel = valor will become global in "all files . js", but in your case you are making a redirect, so all values defined will be lost.

So if that were the case, you should use Cookies, or localStorage to store data you want to use on "another screen".

BUT, apparently you don’t need to do this, because apparently you want the "name of the screen" or is the name of the file .html that you find yourself at the time, from which you can be rescued the time you wish just by using the location.href:

var addr     = location.href;
var nomeTela = addr.substring(addr.lastIndexOf('/')+1, addr.indexOf('.html'))
alert(nomeTela);
  • Paulo Roberto, the example of localStore meets exactly what I need. Thanks for the help too.

  • Okay, but try to explain better what you really want, because your question is confused, containing different understandings.

Browser other questions tagged

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