Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

doing something wrong with global variables?

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
0
0
GB
I think I am missing a basic thing in my JS recently.
I’m trying to use a variable globally, but it doesn’t seem to be global.

I am defining it here, which is within the <HEAD> tags:

Code:
<script type="text/javascript">
<!--
if (drawingsfolder == null){
	var drawingsfolder = "file:///W:/SolidWorks/PDM/Drawings/";
	}
var basefolder = "file:///W:/SolidWorks/PDM/Drawings/";
// -->
</script>

(I added the if null then declare just to ensure it wasn’t re-interpreted unexpectedly)

I have this function (extra irrelevant code snipped out):
Code:
<script type="text/javascript">
function SelType()
{
	if (DWGtype.value==='Packing')
	{
		document.getElementById("secondLevel").innerHTML = "Not required";
		document.form1a.secondSelect.options[0]=new Option("NULL", "NULL", true, false);
		document.getElementById("thirdLevel").innerHTML = "Not required";
		document.form1a.thirdSelect.options[0]=new Option("NULL", "NULL", true, false);
		var drawingsfolder = basefolder + 'Packing/' + product.value;
		alert("path set to: "+drawingsfolder);
	}
}  
</script>

Not surprisingly, the alert pops up with what I what I expect.

Then in my BODY I have added to a random line a test alert here:
Code:
<td width="40"><label><input name="ONEgroup" id="ONEradio1" value="True" onclick="nag(); changeRadio(this, 'ONE', true, 'green', 'black'); ONEcomments.value = ''; alert(drawingsfolder);" type="radio"></label></td>

This pops up with “file:///W:/SolidWorks/PDM/Drawings” AFTER the alert in the function has displayed the full path created.

This leads me to believe the drawingsfolder variable is not global?

I’m probably missing something basic here?


Steve (Delphi 2007 & XP)
 
Hi

There are 2 drawingsfolder variables : a global and a local one.

The [tt]var[/tt] keyword creates a variable in the current scope. Do not use it inside the [tt]function[/tt] declaration if you not want to create a local variable.

Feherke.
[link feherke.github.com/][/url]
 
that makes sense.

How can I concatenate the function added parts of the string I want to build without making the variable local?

Steve (Delphi 2007 & XP)
 

As feherke says;

Leave off the 'var' keyword and the function will use the global object.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top