Click to See Complete Forum and Search --> : retaining a variable outside of a function


LittleRed
09-30-2003, 04:22 AM
I've got a pop-up window which then opens a second pop-up, the first acting as the controller for the content in the second.
I've used a function in the first pop-up to work out the positioning and then open the second with

var variable_name=window.open( ...

and then in the <body> I've got my

onClick="variable_name.document.image_name.src=' ...

which doesn't work, because the 'variable_name' doesn't exist outside of the function. If I remove the 'var' it works fine, but I've heard that there are some problems with variables not declared using 'var' on some versions of IE (especially on Macs).

Is there a way to have the variable retained outside of the function?

thanks!

Charles
09-30-2003, 04:56 AM
Originally posted by LittleRed
Is there a way to have the variable retained outside of the function?

<script type="text/javascript">
function foo () {
fooVariable = true;

// or

window.fooVariable = true;
}

Don't worry about the 2% of users who use Macs out there, worry about the 13% of users who don't use JavaScript at all.

LittleRed
09-30-2003, 05:10 AM
sorry - could you just clarify that,

do I declare my variable first and then include the '...=true'?

so within my function, I'll have

var variable_name;
variable_name = true;

and then it can be referenced outside of the function?


but yeah, I understand not everyone can/will use JavaScript (is it really as high as 13%?) - and I'm trying to ensure that they'll still be able to view the content, even if it's not presented in my pop-ups

thanks for you help

Charles
09-30-2003, 05:14 AM
If you use the "var" inside of a function then the variable will be a part of the call object and will be destroyed when the function exits. If you want it to persist then do not use the "var".

For stats see http://www.thecounter.com/stats/2003/May/javas.php.