1

In my page I get user fb_id in one of function function1() when user perform login action.

Later when user click on some button another function call takes place which calls function2(). I want to fetch value of fb_id in function2(). How can i do it?

structure is like below:

<html>
<head>
<script> 
function fuction2()
{
// some process
var id = fb_it; // Getting fb_it from function1()
}

</script>
</head>
<body>
<script>
function function1()
{
    var fb_it='xyz';
}
</script>

<button type="submit" onclick="function2()"> GetID </button>
</body>

Is it possible or not? If yes then how?

2
  • note that you can't declare a variable with int fb_it='xyz';. Use the var keyword. Commented Oct 18, 2013 at 7:20
  • @dystroy: Yes, it was typo Commented Oct 18, 2013 at 7:22

3 Answers 3

3

No you can't directly access a local variable from an external scope.

What you can do is to store the variable externally :

function fuction2() {
    var id = fb_it; // Getting fb_it from function1()
}
var fb_it;
function function1() {
    fb_it='xyz';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was not knowing global var declaration concept in JS
1

why don’t you use global variables?

<script>
var x = 0;
function(){
x = 7;
}
function(){
x= 8;
}
</script>

Comments

1
function function1()
{
    var fb_it='xyz';
return fb_it;
}

function fuction2()
{
// some process
var id = function1(); // Getting fb_it from function1()
}

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.