Forum Moderators: open

Message Too Old, No Replies

share variable in imported scripts

         

garann

9:22 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



I have two .js files being imported into a page. One defines a function and the next overrides it. This is working as expected, but the overriding function needs access to a variable defined in the first file. This variable gets its value from some inline Javascript on the page.

When I try to refer to the variable, it's not null, just empty, so the second file is apparently aware of the declaration. However, it doesn't see that its value has been updated. Is there a way I can use the DOM to retrieve the current value of the variable?

Thanks!
g.

Bernard Marx

11:03 pm on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the scripts are set up as you describe, then this behaviour shouldn't happen.

Two script blocks/files are the same as one large one, with the exception that you can't reference a function declared in the second block in inline script in the first. This exception doesn't come into play here.

Of two external scripts, the second isn't supposed to start executing until after the first. Very occasionally I have seen buggy hints that suggest that something other was happening.

In the second script, if you try testing your variable:

if(myVar){...}

and you get away without an error message, then it has indeed been declared (and at least set to undefined) somewhere else. Odd.

garann

11:18 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



Yeah, that's what happens. If I use this line:
if (resultSets) alert("variable exists");

I get an alert.

If I use this one:

if (resultSets.length) alert("variable exists");

I don't.

Here's some abbreviated code...

Import 1

var resultSets = new Array();
function setTableWidth() {
if (resultSets.length > 0) {
...
}
}

Import 2

window.setTableWidth = new function()
{
if (resultSets) alert("variable exists");
if (resultSets.length > 0) {
...
}
}

Inline

<script type="text/javascript">
resultSets.push
('frmTest:_id1','divHeadfrmTest:_id1','divScrollfrmTest:_id1');
window.onload = setTableWidth;
</script>

Bernard Marx

3:53 pm on Jan 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (resultSets.length) alert("variable exists");

That one won't fire if the array has length, zero (=> false)
but it looks like you're pushing in elements before that.

Try some closer looks

alert(typeof resultSets)
alert(resultSets.constructor)

Bernard Marx

4:00 pm on Jan 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hang on...what's this!?

window.setTableWidth = new function() 
{
..blah..
}

// from this point on..
setTableWidth isn't a function it's an plain "custom" JS object. The anonymous function is it's constructor.

Either (1) Take away the

[blue]new[/blue]
operator, or (2) write it as a function declaration in the usual way (Same as the function in the block above. It will still override it).

garann

6:51 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



That fixed it. Thanks so much! If you don't mind, could you explain why changing it from a function to an object resulted in it being able to access the variable?