/    Sign up×
Community /Pin to ProfileBookmark

Possible to declare a global variable from within a javascript function?

Hi,

Is it possible to declare a global variable from within a javascript function?

How can it be done?

Any advise is appreciated.

Pardon me for me limited knowledge for javascript.

Thanks

Regards,
Jellybean
?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JellybeanauthorDec 22.2004 — Hi,

Now I know that

var x="5"; //local variable

x="5"; //global variable

I need your help on this:

Basically I have these 2 functions. Each being called on a different page.

The first page calls function prestr when user clicks a hyperlink and another function updateaddr_to is called in the pop up menu that appears after clicking the hyperlink mentioned earlier.

How is it possible for me to pass the value of textval from prestr to updateaddr_to?

I am thinking of declaring textval as a global variable such that it can be used in the updateaddr_to function. However, it doesn't work.

Guess I have to call function prestr again in updateaddr_to. However, I doubt it can be done, since document.replyauthor.to_email.value will generate an error...


function prestr() {

textval = document.replyauthor.to_email.value;

alert(textval);

}


function updateaddr_to(){

var listStr = textval;

nAddr = document.getElementsByName('address');

if (listStr != ''){

for (i=0; i<nAddr.length; i++)

{if (nAddr[i].checked == true){listStr += "," + nAddr[i].value}}}



else{

for (i=1; i<nAddr.length; i++)

{if (nAddr[i].checked == true){listStr += nAddr[i].value + ","}}



if (nAddr[nAddr.length].checked == true){listStr += nAddr[i].value}

}



opener.replyauthor.to_email.value = listStr;

self.close();

}





I am very confused now.



Any advise is most appreciated.



Thanks for reading.



Regards,

Jellybean
Copy linkTweet thisAlerts:
@sciguyryanDec 22.2004 — Yes, its simple.

Declare the variable outside the function and then use the variable without the var keywork inside the function, that will update the global function.

Example:

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
var Example = "";
function Bla(){
Example = "Test";
}
Bla();
window.alert(Example);
//--&gt;
&lt;/script&gt;


The alert wil display "Test".

RyanJ
Copy linkTweet thisAlerts:
@JellybeanauthorDec 22.2004 — Hi,

thanks for your suggestion.

However, I am not too sure what you meant.

Could you slightly relate it to how I can use it in my case?

Thanks a million!
Copy linkTweet thisAlerts:
@JellybeanauthorDec 22.2004 — Hi,

I can't call this function again, can I?


function prestr() {

textval = document.replyauthor.to_email.value;

alert(textval);

}

FYI,

I am developing something similar to a email system.

In this case, I need the first function prestr( ) to retrive existing text from a given textbox and the other function function updateaddr_to( ) to combine this existing text with an array of addresses.

If you can recall the Yahoo! email system, this is how you select the email address of the recipients.

You can type some email addresses in the textbox and click on the address book to add on some more addresses.


What I need to do now is to transfer the exsiting text in the textbox to the second function function updateaddr_to( ), such that I can combine this text with the email addons from the address book, before I output the entire string back to the text box.

I hope you can understand what I mean.


Please take a look at this if you find me unclear.

http://www.webdeveloper.com/forum/s...;threadid=51870

Thanks and good day!
×

Success!

Help @Jellybean 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.25,
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,
)...