View Full Version : Can I use variables to name functions?
spiderman
10-10-2002, 05:35 PM
Background of the Problem:
I am using a proprietary script (wir script) to insert JavaScript onto a webpage. The wir script provides variables (School Names) that link to other pages. I am using JS to open a small window without the menu/tool bars. The script works when the variable is only one word (ie: Smallville). However, when the variable is two words (ie: New Haven), the JS fails.
The Problem:
I need to know if can use a variable name from JS to name a function.
Script Example:
I am using the following code to create short distinct function names but I am having trouble using the JS variable to name the function.
After the wir script is ran, the code looks like this:
<SCRIPT>
var titleName = "New Haven";
var firstLetter = titleName.charAt(0);
var functName = firstLetter;'
var secondLetter = titleName.charAt(1);
var functName = functName+secondLetter;
var thirdLetter = titleName.charAt(2);
var functName = functName+thirdLetter;
var nextLastLetter = titleName.charAt(titleName.length-2);
var functName = functName+nextLastLetter;
var lastLetter = titleName.charAt(titleName.length-1);
var functName = functName+lastLetter;
function +functName+Window()
{
scoreWindow = window.open("New Haven_Smallville.htm", "ScoreWin", "width=625,height=300")
}
</SCRIPT>
I call the function like this
<a href="javascript:functName+Window()">Scores</A>
All help is greatly appreciated and if there is a simpler way of doing this, please let me know.
Special Note:
JS's that contain the single quote (') are not usable as this character is reserved for the wir script.
beetle
10-10-2002, 06:37 PM
Uh, yes. There is certainly a better way to handle this....var titleName = "New Haven";
openWindow(titleName);
function openWindow(t) {
var nUrl = "";
switch(t) {
case "New Haven" : nUrl = "New Haven.htm"; break;
case "Smallville" : nUrl = "Smallville.htm"; break;
case "High School1" : nUrl = "hs1.htm"; break;
// Insert as many cases in this fashion as you need
default : alert("School not found!");
}
if (nUrl != "")
scoreWindow = window.open(nUrl, "ScoreWin", "width=625,height=300");
}
spiderman
10-10-2002, 08:35 PM
Thanks for the quick reply beetle. However that did not correct my situation. The JS is generated from the wir script in a loop. Hence for each link, there is a respective function.
Allow me to show you an example of the wir script.
!RACE START
buffer ' <TR>' crlf
buffer ' <TD align="center" WIDTH="50"><STRONG>' [title1] '</STRONG></TD>' crlf
buffer ' <TD align="right" WIDTH="350"><STRONG><FONT FACE="ARIAL">' [title2] '</FONT></strong></TD>' crlf
buffer ' <TD align="center" WIDTH="45">vs.</TD>' crlf
buffer ' <TD WIDTH="350"><STRONG><FONT FACE="ARIAL">' [title3] '</font></STRONG></TD>' crlf
buffer ' <TD align="center" WIDTH="50"><STRONG>'crlf
buffer '<SCRIPT LANGUAGE="JavaScript" TYPE="Text/JavaScript">' crlf
buffer ' <!--' crlf
buffer ' var titleName = "' [title2] '";' crlf
buffer ' var firstLetter = titleName.charAt(0);' crlf
buffer ' var functName = firstLetter;' crlf
buffer ' var secondLetter = titleName.charAt(1);' crlf
buffer ' var functName = functName+secondLetter;' crlf
buffer ' var thirdLetter = titleName.charAt(2);' crlf
buffer ' var functName = functName+thirdLetter;' crlf
buffer ' var nextLastLetter = titleName.charAt(titleName.length-2);' crlf
buffer ' var functName = functName+nextLastLetter;' crlf
buffer ' var lastLetter = titleName.charAt(titleName.length-1);' crlf
buffer ' var functName = functName+lastLetter;' crlf
buffer crlf
buffer ' function openWindow(functName)' crlf
buffer ' {' crlf
buffer ' scoreWindow = window.open("' [title2] '_' [title3] '.htm", "ScoreWin", "width=625,height=300");' crlf
buffer ' }' crlf
buffer ' document.write ("<a href=javascript:openWindow(" +functName+ ")>")' crlf
buffer ' -->' crlf
buffer '</SCRIPT>' crlf
buffer 'Scores</A></STRONG></TD>' crlf
......YADA YADA YADA...........>
!END
When this code loops, it is supposed to generate a link to each "game score" page that has been generated. This worked until I started inserting the JS into the wir script.
My attempt was to generate a page that is small enough to leave open in the corner of the user's desktop. Here (http://www.kwtx.com/sports/sports_friday/sf_scores/10_4_02_wk5/default.htm) is an example of how the code worked before I started inserting the JS.
One other thing that I could try is using a JS trim function (if one exists). I have looked but have yet to find one.
Thanks for any suggestions.
beetle
10-10-2002, 09:21 PM
Ok, parameters exist in JS (and other languages) to avoid this sort of nonsense. First of all, you are using ham-fisted method to create the functName variable, which should be titleShort or something like that (I think)...the code below replaces the 10-some lines you've got to achieve this...var titleName = "New Haven";
var titleShort = titleName.replace(/^([\w ]{3}).*([\w ]{2})$/,"$1$2");Now..moving along...how exactly is the variable we just made (titleShort) related to the HTML file that opens in the popup?? (in your wir script it seems to be populated by the varables [title2] and [title3]) So it seems that you've done nothing here with the titleShort variable...(or functName as you call it)
So, most importantly...what is the relationship? What changes from popup to popup besides the HTML file that is loaded for that school?
spiderman
10-10-2002, 09:50 PM
So...I can use that string of JS (that you wrote) to do the same thing that my 10 lines of code does...Great! I'll do that.
Now the shortTitle (or functName) variable that we have established is supposed to change each time that the wir script loops.
[title1] represents the class (5A, 4A...6MAN, TAPPS)
[title2] represents the Visiting Team
[title3] represents the Home Team
I was using [title2] to establish the function name
<JS>function ' [title2] 'Window()</JS>
worked when
[title2] = Smallville (ie: function SmallvilleWindow())
but didn't work when [title2] = New Haven (ie: function New HavenWindow())
The school names are pulled from a database that is re-populated each week. So to make data entry easier, I wanted to code it so that it would pull the first three and last two letters of [title2] (thus eliminating the space factor). The problems began when I started trying to call the function using the variable functName.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.