/    Sign up×
Community /Pin to ProfileBookmark

javascript global variables

Hi,

I have a custom toolbar in firefox which calls js functions from an external javascript file.

In the js file I declared a global variable:

var test;

function testfn1()
{
var test = event.innerHTML;
}

function testfn2()
{
var testing = test;
alert(testing);
}

I first call the function testfn1 when i click on a button on the toolbar and then when I click on another button on the toolbar, it calls testfn2. However I noticed that the function testfn2() alerts “undefined”. When I embed all this in the html page, it works fine; however in the external js it seems its not working.

any ideas?

Thanks

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineApr 06.2009 — 
var test;

function testfn1()

{

var test = event.innerHTML;

}
[/QUOTE]
The global [I]test[/I] is declared as undefined, and you're not assigning it a value anywhere in the code shown.
Copy linkTweet thisAlerts:
@Csam0003authorApr 06.2009 — even if i assign it a value as shown below:


var test;

function testfn1()

{

var test = "testingtesting";

}

it still doesnt work!
Copy linkTweet thisAlerts:
@ZeroKilledApr 06.2009 — it display undefined because the variable in [b]testfn1[/b] is local to that function:

[b]var[/b] test = "testingtesting";

if you remove the [b]var[/b] statement it should work.
Copy linkTweet thisAlerts:
@Csam0003authorApr 06.2009 — Thanks for your reply.

That was a mistake though. without the var it still doesnt work

var test ="";

function testfn1()

{

test = "hello hello";

}

function testfn2()

{

alert(test);

}
Copy linkTweet thisAlerts:
@ZeroKilledApr 06.2009 — are you calling [b]testfn1[/b] before [b]testfn2[/b]? otherwise you get an empty alert.
Copy linkTweet thisAlerts:
@Csam0003authorApr 06.2009 — yes! Maybe its an issue with external files?
Copy linkTweet thisAlerts:
@ZeroKilledApr 06.2009 — i don't think so. if you place the code on an html document you will see the code work correctly. so, the problem should be in other place but not in the code.
<i>
</i>&lt;script&gt;
var test ="";

function testfn1(){
test = "hello hello";
}

function testfn2(){
alert(test);
}
testfn1(); testfn2();
&lt;/script&gt;
Copy linkTweet thisAlerts:
@JMRKERApr 06.2009 — This proves it works. ?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;test script&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=995876#post995876

var test ="Global";

function testfn1() {
test = "Local";
alert(test);
}

function testfn2() {
alert(test);
}

function Initialize() {
test = 'Global';
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;button onclick="testfn2()"&gt;Global&lt;/button&gt;
&lt;button onclick="testfn1()"&gt;Local&lt;/button&gt;
&lt;button onclick="Initialize()"&gt;Initialize&lt;/button&gt;
&lt;br&gt;
To see actions:
&lt;ol&gt;
&lt;li&gt;Press Global&lt;/li&gt;
&lt;li&gt;Press Local&lt;/li&gt;
&lt;li&gt;Press Global again (changed value)&lt;/li&gt;
&lt;li&gt;Press Initialize to do it again!&lt;/li&gt;
&lt;/ol&gt;
&lt;/body&gt;
&lt;/html&gt;

What is does not show is what you are doing wrong ???? ?

Show us how it is not working for you. :eek:
Copy linkTweet thisAlerts:
@Csam0003authorApr 07.2009 — Hi,

thanks alot guys for your input. I just realized that in myscript the first function (i.e.testfn1) is called by onmouseover. Let me explain: Basically I have this:

<span id= "something" onMouseOver = "testfn1()" >dog and cat </span>

testfn1() is then called when the user hovers over the text which then calls testfn2(). This displays a blank alert! It seems that the onMouseOver is the issue here as when I did both calls through seperate buttons all worked well!

any ideas how i can save a value in a global variable using onmouseover?

thanks
Copy linkTweet thisAlerts:
@ZeroKilledApr 07.2009 — testfn1() is then called when the user hovers over the text which then calls testfn2().[/quote]

you mean that testfn2 is called inside testfn1? can't understand when testfn2 is been called. post your current html code. i don't think that the type of event affect the script. most important is the order in which they are called.
Copy linkTweet thisAlerts:
@Csam0003authorApr 07.2009 — guys,

I realized that it is an issue with the xul file I have. Basically I have testfn1 called when the user hovers over some text (i.e. onmouseover) and then I use an Xul button to call testfn2. It seems the global variable resets.

anyways I managed a workaround. I thank you all for your input and help

kind regards

chris
Copy linkTweet thisAlerts:
@JMRKERApr 07.2009 — What the heck is a "Xul button"? For that matter, what is a "xul file"?
Copy linkTweet thisAlerts:
@ZeroKilledApr 07.2009 — XUL = XML UserInterface Language. that is the language that firefox use to show up the browser's UI.

http://www.mozilla.org/projects/xul/

http://en.wikipedia.org/wiki/XUL
×

Success!

Help @Csam0003 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.16,
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,
)...