Click to See Complete Forum and Search --> : First self-made script... not working


Zifnab
07-01-2003, 12:49 PM
Below is the first function(s) I've made in JavaScript completely from scratch, and it doesn't work. The error I get from this(these) function(s) in Mozilla's JS debugger is that "locArray" is undefined. There might be more errors, but for now the debugger doesn't show any more:


function changeLang()
{
menuhref = parent.menuBar.location.href;
parent.menuBar.location.href=setMain(menuhref);
toplhref = parent.topL.location.href;
parent.topL.location.href=setMain(toplhref);
foothref = parent.footer.location.href;
parent.footer.location.href=setMain(foothref);
mainhref = parent.mainCon.location.href;
parent.mainCon.location.href=setMain(mainhref);
};

function setMain(pagelink)
{
locArray = pagelink.split("/");
last = locArray.length - 1;
oldLoc = locArray[locArray.length - 1];
document.write(oldLoc);
if (locArray[last].charAt(0) = "s")
{
lang = "e";
}
else
{
lang = "s";
}
newLoc = lang + locArray[last].substr(1); return newLoc;
};


This JS gets loaded into the HTML through:

<script language="JavaScript1.2" src="langSwap.js"></script>

And is obviously in the file langSwap.js...
The link that's supposed to make this go off is:

<a href="javascript:changeLang()"><anImageHere></a>

What this function is supposed to do is:
1) get the path names to the files in the different frames
2) modify the file names depending on their current name (ie. change first letter from "e" to "s" or viceversa)
3) load the "new" pages in those frames...

I've been looking at Netscape's Devpage JavaScript-Manuals and so on but haven't managed to find what is wrong with this script... if anyone could help me out it'd be much appreciated.

Jona
07-01-2003, 01:14 PM
Do you perhaps have a link?

[J]ona

Zifnab
07-01-2003, 01:19 PM
uhmm.. bugger.. nope.. did the testing locally... :(
Darn... well, I'll put it up as soon as I can (won't be for at least a couple of hours, have to run now)...

Uhmmm... if there is no instance of the separator the array would still have at least 1 value right? (the full variable value?) So that shouldn't make it undefined? Just asking because I just realized, local testing would possibly give "\" instead of "/" between the directories and so forth... so no separators... :D

Zifnab
07-02-2003, 05:09 AM
Ok... here's a link to the testpage... haven't uploaded any of the images though... it's the left "missing image" on that yellow bar that calls the javascript function...
http://www.sscdata.com/test/testje.html

Zifnab
07-02-2003, 07:23 AM
Fixed... working code:

function changeLang(newLang)
{
menuhref = parent.menuBar.location.href;
parent.menuBar.location.href=setLang(menuhref,newLang);
...
//Changed the changeLang function so more than
//2 "language options" were available...

function setLang(pagelink,lng) {
urlsplit = pagelink.split("//");
docsplit = urlsplit[1].split("/");
docname = docsplit[docsplit.length -1 ];
newloc = lng + docname.substr(1);
return newloc;
};

(this being th replacement for the "setMain" function)

Problems with old code:
"lang" not a usable variable name.
"http://www." the "//" gave problems with the old, single split, so had to split the URL twice to get to the file name.
Original "array undefined"-error seemed to be related to local instead of server testing as the problem dissapeared once it was on the server...

Added all the comments and so for if anyone is/was interested... really just wanted to say it's fixed.. and thanks for the help :D