1

Say I have a variable called "true" in a function called "test." Then I have another function in a whole different script tag and I want to change "true" using my new function. How can I do this? Thanks.

<script type="text/javascript">
var hello="no";
if(hello=="yes"){ 
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "Message";
  }
}
</script>

<script type="text/javascript">
function show(id) {
     $('#' + id).show();
     var hello="yes";


}
</script>

It doesnt seem to be working...

6
  • 4
    It's all about scope. The variable must be defined with var at a higher scope than the functions that use it. Post your code and we can help. Without code, we cannot. Commented Sep 24, 2012 at 1:37
  • Can you create a JSFiddle to demonstrate, true is a reserved word and can't be used for a variable name. It would be nice to see what you're doing. Commented Sep 24, 2012 at 1:38
  • var true; function f1() {true = 'hi';} function f2() {true = "something else";} Commented Sep 24, 2012 at 1:38
  • The variable is not actually named true, ill post code one second. Commented Sep 24, 2012 at 1:39
  • There, I posted the code Commented Sep 24, 2012 at 1:40

2 Answers 2

4

In your function, don't use the var keyword. Doing so declares a different variable hello in scope of the function.

// Earlier, you defined the variable at a higher scope here:
var hello="no";
// The function has *not* been called yet, so hello will never equal "yes" when this initially runs.
if(hello=="yes"){ 
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "Message";
  }
}

function show(id) {
  $('#' + id).show();
  // No var here!
  // the variable was defined at a higher (window) scope already with the var keyword.
  hello="yes";
}

Update:

Your logic is faulty when calling the onbeforeunload. You are never binding the event unless hello == "yes", which it never does when that runs. Instead, check the variable contents in the confirmExit() function:

window.onbeforeunload = confirmExit;
function confirmExit()
{
  if (hello == "yes") {
    return "Message";
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@CJSculti Unless you called the function show(), the variable won't be set at the time it is initially called in the first script block.
I do call the function show. I call it onclick of a button. The point is so the box will only work after the button is pressed.
And the confirmExit function is called onunload of body, so that should be called last, no?
0
// this will work in your case 
var hello="no";
if(hello=="yes"){
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "Message";
  }
}
function show(id) {
     $('#' + id).show();
     hello="yes";
}


// Just an small explation of how scope works in Javascript

var hello = "no"; // this has a global scope.
function showGlobal(){
alert(hello); // will alert 'no'
}

function showLocal(){
 var hello ="yes"; // hello is now a local variable. Scope is limited to the function.
 alert(hello); // will alert 'yes'. Scope is local  

}

function showLocalBlock(){

    if(condition == 'true'){
    hello = 'yes';
    }
alert(hello); // will alert yes.

}

// Closure

function ShowClosure(){
 var hello = "yes";
    function ShowInner(){
    alert(hello); // here the variable hello takes the value of the function in which it was defined.

    }  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.