473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Global Variable In A Function

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
4 5544
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
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


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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.