Skip to content Skip to sidebar Skip to footer

Global & Local Variables In Javascript

I have one variable and two functions . The variable is used by both. and the first function is changing the variable value (globally) each time it's used by it . This is what I wa

Solution 1:

The problem is the time when you execute the code. The uplodify options are set on page load (which includes that P is passed on page load) and as P is a string, changing P later (through save()) will not change the value you passed.

You can solve this by passing a reference to the object as option, instead of the string Edit: Didn't work.

The plugin provides a uploadifySettings method to change the settings of an uploadify instance. Use it to update the scriptData settings:

functionSave() {
    $('#btnBrowse').uploadifySettings('scriptData', {'Album_ID' : guid()}, true);
    $('#btnBrowse').uploadifyUpload();
}

Solution 2:

Maybe this fiddle can help you understand global scope a little better: http://jsfiddle.net/XFx27/1/

var x = 0;

function add1()
{
 x = x + 1;
}

function show()
{
    alert(x);
}

add1();
show(); //alerts 1
add1();
show(); //alerts 2

Your missing parens () after your functions function funcName()

Solution 3:

x = 1;

function f1()
{
  x = x + 1;
  // use x 
} 

function f2()
{
  // use x
}

// x is 1

f1();

// x is 2

f2();

Solution 4:

firstly, the f1 function should be:

function f1(){
   x = x + 1;
   // use x
}

var x;

functionf1(){
    x = x + 1;
    console.log(x);
}

functionf2(){
    console.log(x);
}

f1(); // 2f2(); // 2

I tried the code in chrome console and I think it really works.

Post a Comment for "Global & Local Variables In Javascript"