Forum Moderators: open

Message Too Old, No Replies

New to Javascript

Getting the first subfolder from URL string

         

sheel331

1:57 pm on Mar 3, 2006 (gmt 0)

10+ Year Member



Hello all,

I'm very new to Javascript, and I'm having some issues where maybe someone could help me out. I'm trying to create a simple html page which has javascript embedded into it where it will get the URL string that the user enters and take the first subfolder and redirect the user appropriately. For instance, if I have the URL of http://example.com/helloworld, I would like to get the "helloworld" subdirectory and redirect that to some other URL. Can this be done? Thanks in advance for any help you all can provide.

Cheers,
Sheel

[edited by: DrDoc at 7:10 pm (utc) on Mar. 7, 2006]

Bernard Marx

2:55 pm on Mar 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var url = "http://myurl.net/helloworld";
/* Remove everything up to & incl the last slash */
var parentDirName = url.replace(/^.*\//,"");

/**/alert(parentDirName )

Not sure what you want to do after that.

DrDoc

1:04 am on Mar 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That won't work, Bernard Marx ...
Directories always have a trailing slash. Also, what if there's a file name or query string?

Something like this, perhaps:

<script type="text/javascript">
var url = location.href.replace('/' + location.search + '$/', '');
url = url.replace(/^.*\/([^\/]*?)\/([^\/]*?\.[^\.]+?)?$/, '$1');
document.write(url);
</script>

sheel331

2:36 pm on Mar 7, 2006 (gmt 0)

10+ Year Member



Thank you very much. I have figured out how to split based on every slash '/'. The code is this:

var myURL, i;

myURL = document.URL;
document.writeln(myURL);

var myArray = myURL.split("/");
alert(myArray.length);

for(i = 3; i < myArray.length; i++)
{
document.writeln("<br> element is" + myArray[i] + "<BR>");

document.writeln(myArray.length)
if (myArray.length > 3)
{
document.writeln(myArray.length)
document.writeln("<br>" + myArray[3])
}
else
{
document.writeln("<br>lets do something")
}
}

So now what I want to do is that I would like to parse through each element in the array(which is the folder names), and verify that the folders exist. If it does, it will redirect the URL to the appropriate URL. So for instance, say I have a URL of http://example.com/sheel/working. I would like to check to see if the sheel folder exists, then from there redirect the working folder to another URL. I realize this is a somewhat complex issue, and thank anyone and everyone in advance for their help.

[edited by: DrDoc at 7:08 pm (utc) on Mar. 7, 2006]

DrDoc

7:09 pm on Mar 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you are making this more complex than it has to be. If that form of redirection is all you need, then you should probably look into mod_rewrite instead ;)

sheel331

2:00 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



Please excuse my ignorance, but isn't mod_rewrite an apache function? The webserver I am using currently is iPlanet web server 6.0.

Thanks,
Sheel

DrDoc

5:10 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are absolutely right. mod_rewrite is an Apache thingy.
Not sure what the iPlanet equivalent is.