cancel
Showing results for 
Search instead for 
Did you mean: 

javascript issue with url object

damonsmith
Champ in-the-making
Champ in-the-making
Hi all, I just found a funny issue with the url.extension object on the base context in my web script.  The javascript native shift() function doesn't work correctly on the url.extension object, but it works correctly elsewhere.

I'm calling my script with a path like: /mycomp/services/imageThinger/dog/cat/ball/bat
so the url.extension would be:
dog/cat/ball/bat

And I want to shift() the storeId off the front of the array, and use the rest as the file path in the store.

so in my web script I say:
    var testArray = url.extension.split("/");
    model.testLengthBefore = testArray.length;
    testArray.shift();
    model.testLengthAfter = testArray.length;

And they both come out the same!!  What's more, the first element has indeed been shifted off the front of the array, but the last one has been duplicated mysteriously, so when I join them back up I get a path that looks like:
cat/ball/bat/bat

I could understand if the split() function wasn't there, and I was operating on some strange java object behind the scenes, but you'd think that calling split() would give me some old array implementation with a working shift function, whether it was just javascript or java.

So a simple fix for this is to replace the first two lines with:
    var test = "" + url.extension; //type coercion into a normal js string object
    var testArray = test.split("/");

And then the length decreases by one as expected and everything works.  I wonder what sort of object url.extension really is, to be doing this weird thing, and whether it's a fundamental Rhino bug or just some weirdness in the API objects used.  I searched Alfresco JIRA for it and found nothing with "web script" and "shift" matching my bug.
1 REPLY 1

opoplawski
Champ in-the-making
Champ in-the-making
I'm seeing the same thing with document.name.  Had to do:


   var filename = new String(document.name);
   var folder = document.parent;
   var elements = filename.split(".");

In order for the split to work.