/    Sign up×
Community /Pin to ProfileBookmark

Variable is undefined after out from the function block

Dear fellow developers,

I would like to know how to use the variable same out from the function block.?

1 var tmp;
2 function handleRequest(obj) {
3 var writeroot;
4 writeroot = obj.responseText;
5 tmp = writeroot;
6 }
7 document.write (tmp);

The variable at line 7 shown me “undefined” message,
please advice how to make the variable tmp have the same value with “writeroot”.

Thank you.

KY

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinMar 21.2007 — where is the line that shows you running the function?
Copy linkTweet thisAlerts:
@Tweak4Mar 21.2007 — The problem is your first line. You are instantiating the variable "tmp", but not giving it an initial value. Since lines 2-6 are a function, they aren't processed immediately, and when line 7 executes, tmp still doesn't have a value (hence the "undefined").

If you replace line 1 with:

var tmp = "temporary value we don't care about";

line 7 will display that.
Copy linkTweet thisAlerts:
@kyfooauthorApr 05.2007 — Dear Tweak4,

I tried the suggestion you given to me.

By assign tempolary string to "tmp" at lione 1 as

var tmp = "tempolary value";

The value shown by line 6 have the value "tempolary value" instead of the new value from the function line 2 to line 6
×

Success!

Help @kyfoo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...