By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,380 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,380 IT Pros & Developers. It's quick & easy.

Global Variable In A Function

P: n/a
I have a .js file that receives a value from an html page. I'd like this
value to be a global variable that all functions in the .js file can use. I
have tried just declaring a var at the top of my .js file, but when the
value comes into the function and gets assigned to that variable, it is not
global.

Any help, hints, etc. would be appreciated.

Thanks,
Sam
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Strange it works for me but the global variable is in the <script> part of
the html document.

jerome

"SamMan" <ps*@psfdev.com> a écrit dans le message de news:
Fi**********************@twister.rdc-kc.rr.com...
I have a .js file that receives a value from an html page. I'd like this
value to be a global variable that all functions in the .js file can use. I have tried just declaring a var at the top of my .js file, but when the
value comes into the function and gets assigned to that variable, it is not global.

Any help, hints, etc. would be appreciated.

Thanks,
Sam

Jul 20 '05 #2

P: n/a
What I'm trying to do is to pass a value from "first.html" to a function in
the .js file (works fine). This value should get assigned to a global
variable (not working) and within the same function, a new window is opened.
"second.html". This second pop-up window then calls (onLoad) another
function within the same .js file and writes the value of the global
variable to the page.

Thanks,
Sam

"Jérôme VUIBERT" <je************@fleximage.fr> wrote in message
news:3f**********************@news.free.fr...
Strange it works for me but the global variable is in the <script> part of
the html document.

jerome

Jul 20 '05 #3

P: n/a


SamMan wrote:
I have a .js file that receives a value from an html page. I'd like this
value to be a global variable that all functions in the .js file can use. I
have tried just declaring a var at the top of my .js file, but when the
value comes into the function and gets assigned to that variable, it is not
global.


If you declare
var varName;
outside of any function then you have declared a global variable which
you can then use as
varName = expression

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4

P: n/a
"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:3F**************@t-online.de...


If you declare
var varName;
outside of any function then you have declared a global variable which
you can then use as
varName = expression


Thanks Martin,

I'm aware of what you are saying, but the problem I'm having is that unless
I initialize the var (var varName = "someValue"), the variable remains null,
or undefined. In the code below, when "text" is assigned to "print", it is
not global, and "print" retains the value of "null".

Thanks for the help,
Sam

var print="null";

function testWindow(text) { //called by first page
print = text;
alert("first -" + print)
var newWin;
newWin = window.open("dataPage.html","", "height=80,width=250");
}

function passVar() { //called by pop-up page
alert("second function -" + print);
document.write(print);
}
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.