?
Solved

Separating a portion of the URL

Posted on 2009-04-22
4
Medium Priority
?
270 Views
Last Modified: 2012-05-06
I would like to retrieve a particular folder's name from the page's URL.

The url looks something like this:
http://thewebsite.com/directory/namewanted/

In some cases the folder is the last in the url, in others it is the parent directory.
http://thewebsite.com/directory/namewanted/anotherdirectory/

Using the following code:
<script>
str=window.location.href
name=str.match(/\w+\.hmtl/|html/)
alert(name)
</script>

Open in new window

0
Comment
Question by:spacegypsy
  • 2
4 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 24210591
Try this:
<script type="text/javascript">
function getDir() {
   var myloc = window.location.href;
   var locarray = myloc.split("/");
   return locarray[locarray.length-2];
}
alert(getDir());
</script>

Open in new window

0
 

Author Comment

by:spacegypsy
ID: 24210640
It works like a charm for this URL.
http://thewebsite.com/directory/namewanted/

But for this URL:
http://thewebsite.com/directory/namewanted/anotherdirectory/

It returned "anotherdirectory" instead of "namewanted."
0
 
LVL 14

Accepted Solution

by:
sam2912 earned 200 total points
ID: 24211012
Oh, I thought you wanted the last (current) directory. here:
<script type="text/javascript">
function getDir() {
   var myloc = window.location.href;
   var locarray = myloc.split("/");
   return locarray[4];
}
alert(getDir());
</script>

Open in new window

0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 24214759
if it is **always** in the same position you can use this
window.location.href.split('/')[4]

alert(window.location.href.split('/')[4])

need to wrap it in an if or try catch to avoid errors when the url is not going to be able to be split into 5 items
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question