If you've not looked at the source JS code it is listed below, at the bottom of the page.
The function takes the form "function makeNewPath(inputRef, rootName, newRoot)" where:
- inputRef is a path string - most likely the PortWeb macro %path% - but passed as a value to an HTML element, e.g. an <input> element. This nuance is the thing most likely to trip the newbie. I'm not certain but I think this is designed to circumvent the issues surrounding handling the backslashes in Windows paths as the character is also JavaScript's escape symbol. If all this talk of references confuses you, I offer a revised version which takes a (path) string argument instead of inputRef. See makeNewPathStr function code below.
- rootName is the name of the LAST folder in the source path string before the point where you wish to split the string. So for "Vol:this:that:other:file.jpg" if rootName is "that", the function returns "/other/file.jpg". This is demonstrated in Examples #1-#4 using the varying different styles of path Portfolio may provide (Mac style#2 is the most common - Portfolio's 'native' format). Look at the page's source code to see when is actually being submitted.
- newRoot allows you to add a a new string to the front of the returned path. In the previous example, if newRoot was "/previews" the result would be "/previews/other/file.jpg". Example #5 demonstrates this feature.
Once you've seen the demos, try out your own data in Example #6.
If you'd like to run the makeNewPath code as an external JS file - instructions are included in the source code for this page
Note the function does not join the root and the substrng path it creates. This is deliberate. Issues you may need to figure out is where your pages are rooted. In Mac WebStar PortWeb pages are in the root whereas in Windows IIS they are - normally - in root/scripts/ so you may need to do a little extra scripting as the relative path from root to root/previews/ is "previews" wheras from root/scripts/ it is "/previews". So why does the default returned path fail on a Mac. The root is a special case - you can't go one folder up - so a relative path of "/previews/pic.jpg" from the root only. If you're lost be this go and read up on realitve web paths and use of the / and ../ notations (try Google).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//IMPORTANT - don't forget there is a helper function listed below
// after the main function
function makeNewPath(inputRef, rootName, newRoot)
{
// inputRef = reference to a form element holding the path value
// rootName and newRoot are string values - confusing!
// do this: makeNewPath(myForm.theInputRef,myForm.theRootName.value,myForm.theNewRoot.value)
// not : makeNewPath(myForm.theInputRef.value,myForm.theRootName.value,myForm.theNewRoot.value)
// ... the difference is don't interrogate the inputRef's value - the makeNewPath does that
var dPath;
var startIdx, replaceIdx, endIdx;
var strArr, idx;
orgPath = getLastInput(inputRef);
// clean the original path separators for the web
var clnPath = (orgPath.split("\\")).join("/");
clnPath = (clnPath.split(":")).join("/");
startIdx = clnPath.indexOf(rootName);
if (startIdx >= 0)
{
// strip off everything up to the root name
startIdx = startIdx + rootName.length;
dPath = newRoot + clnPath.substring(startIdx, clnPath.length);
// Encode each part of the path into web-ready form
strArr = dPath.split("/");
for (idx=0; idx<strArr.length; idx++)
{
strArr[idx] = escape(strArr[idx]);
}
dPath = strArr.join("/");
}
else
{
dPath = "";
}
return dPath;
}
// Utility function for makeNewPath
function getLastInput(inputRef)
{
if (inputRef.length == null)
{ return inputRef.value; }
else
{
var curIdx = inputRef.length - 1;
return inputRef[curIdx].value;
}
}
To top of page.
This code is included in the script in this page's <head> code.
//IMPORTANT - the previous version's helper function is now deleted
function makeNewPathStr(origPath, rootName, newRoot)
{
// origPath, rootName and newRoot are string valuess
// do this: makeNewPathStr(myForm.theInputRef.value,myForm.theRootName.value,myForm.theNewRoot.value)
// or just pass strings: makeNewPathStr(origPath, rootName, newRoot)
// declare all the variables we'll use
var dPath, startIdx, strArr, theIdx, clnPath;
// clean the original path separators for the web
clnPath = (origPath.split("\\")).join("/");
clnPath = (clnPath.split(":")).join("/");
startIdx = clnPath.indexOf(rootName);
if (startIdx >= 0)
{
// strip off everything up to the root name
startIdx = startIdx + rootName.length;
dPath = newRoot + clnPath.substring(startIdx, clnPath.length);
// Encode each part of the path into web-ready form
strArr = dPath.split("/");
for (theIdx=0; theIdx<strArr.length; theIdx++) {
strArr[theIdx] = escape(strArr[theIdx]);
}
dPath = strArr.join("/");
}
else
{
dPath = "";
}
return dPath;
}
To top of page.