0

I have initialized a variable:

var isTrueOrFalse; 
//Called when button add in page1 is clicked
function ExecuteThis(){
   isTrueOrFalse = true;
}
//called when button remove in page1 is clicked
function ExecuteThat(){
 isTrueOrFalse = false;
}

Then I have a function that validates if isTrueOrFalse is false or true

//Called when page2 onLoad
function NowChange(){
 if(isTrueOrFalse == true){
 //dostuff
 }else{
 //do this stuff instead
 }
}

The console returns that isTrueOrFalse is undefined. So my question is, how do I define a global variable inside a function and pass the value of it to another function?(NowChange())

Mnemonics
  • 679
  • 1
  • 10
  • 26
  • Globals should be defined explicitly: `window.isTrueOrFalse` Relying on auto-globalling of variables is bad. Then again, global variables in general are bad... – Niet the Dark Absol Dec 09 '14 at 16:57
  • possible duplicate of [Define global variable in a JavaScript function](http://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) – Scimonster Dec 09 '14 at 16:57
  • I don't understand your problem. Where/How are these functions called? What unexpected results do they give? Why does `isTrueOrFalse` need to be global? – Bergi Dec 09 '14 at 16:58
  • Can't you just initialize your global variable with a sensible value, i.e. either `true` or `false`? – Bergi Dec 09 '14 at 16:58
  • @NiettheDarkAbsol: The variable is explicitly declared with `var` in that higher scope. Everything is fine, no need to use `window.`. – Bergi Dec 09 '14 at 16:59
  • @Bergi NowChange is called in the second page(onload="NowChange()"). If the user has clicked the button add, the input(in second page) should change it's text to Add otherwise to Delete. ExecuteThis is executed when button Add is clicked and ExecuteThat is executed when button delete is clicked. – Mnemonics Dec 09 '14 at 17:03
  • 1
    "second page"? Pages do not remember each other's javascript variables. Each page javascript environment is created afresh with only variables/functions/files declared/loaded by this page. You will have to pass this value to the second page through the server-side code or query string. – Igor Dec 09 '14 at 17:04
  • @Bergi if i initialize it to true only the code in between if(isTrueOrFalse == true) is executed. – Mnemonics Dec 09 '14 at 17:05
  • I think what you really want are submit buttons with values. Don't use JavaScript click handlers for this. – Bergi Dec 09 '14 at 17:06
  • @Igor hmm okay that explains everything. Then how can I change the text depending on which button i have clicked in previous page? Thanks for the help. – Mnemonics Dec 09 '14 at 17:06
  • 1
    @Mnemonics - I added to my previous comment. – Igor Dec 09 '14 at 17:08
  • @Igor would it work if I used localStorage? I still get null as result. – Mnemonics Dec 09 '14 at 18:08

1 Answers1

0

//Called when page2 onLoad, see your comment one next page load your variable will be re-initialized, and you are not assigning any value in your first line so it is undefined

Hope it can help you

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85