How to make a jquery global variable?

Asked

Viewed 203 times

0

I have this following variable:

Site = {
   Alert: function(titulo, conteudo, data){
      var id = Math.round(Math.random()*10000);
   }
}

I’d like to access it anywhere in my code, specifically here:

User = {
   ConfirmSair: function(){
      alert(id);
   }
}

How can I do that?

1 answer

-1


At times var id uses window.id, or a more secure name not to be overwritten as window.randomId.

This type of programming is not recommended because it generates code difficult to maintain, read and is conducive to over-write this variable elsewhere in the code and you do not know where it is happening.

But if you need stay the suggestion as I indicated above.

  • n worked that your way there.

  • @Paulosérgiofilho can you explain what did not work? My suggestion does not work unless you have understood my answer. But if you create an example that reproduces your problem I can show you how to make it work.

Browser other questions tagged

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