Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

Extract folder name from URL path

Would like to extract part of the URL of each page to add navigation links that users can click to reach the root of parent folders.

Creating crumbs trail navigation such that for a page with URL domain.com/store/specials/, each of the words in the trail would be a link:
Home > Store > Specials  .. would reference:
domain.com > domain.com/store/ > domain.com/store/specials

I want to use javascript to automatically create the links in "Home > Store > Specials" by examining the URL of the page.
0
controltheweb
Asked:
controltheweb
  • 6
  • 6
1 Solution
 
b0lsc0ttCommented:
If this is always your site and ends with .com then use ...

var folders = document.location.split('.com/')[1];
var arFolders = folders.split('/');

Each folder will now be in the arFolders array.  Don't use the last item in the array since it will have the filename.

Let me know if you have a question about using this or need more info.

bol
0
 
controlthewebAuthor Commented:
Thanks! Appears I could use some help. Testing it out to try to simply list the values from the array, I tried this without success:

<script language="JavaScript" type="text/JavaScript">
<!--
      var folders = (document.location).split('.com/')[1];
      var arFolders = folders.split('/');

for (i=1;i<arFolders.length;i++)
{
document.write(arFolders[i] + "<br />")
}
//-->
</script>
0
 
b0lsc0ttCommented:
There was one thing missing in my suggestion and one mistake (I think) in how you used it.  Try ...

<script language="JavaScript" type="text/JavaScript">
<!--
      var folders = document.location.toString().split('.com/')[1];
      var arFolders = folders.split('/');

for (i=0;i<arFolders.length;i++)
{
document.write(arFolders[i] + "<br />")
}
//-->
</script>

Notice I start the For loop at 0.  Change it back to 1 if you want to skip the first folder.  Also make sure you are testing this on a page with a URL that has .com in the domain.

Let me know how that works or if you have a question.

bol
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
controlthewebAuthor Commented:
Nice! Pretty slick compared to this alternative: http://webmonkey.com/webmonkey/05/52/index0a_page3.html

How to count the array size so I know how many links I need? E.g.

domain.com/parent/ --> domain.com/parent/child --> domain.com/parent/child/subchild

0
 
controlthewebAuthor Commented:
Starting from i<arFolders.length-1 .. etc
0
 
b0lsc0ttCommented:
You can use arFolders.length.  That will be the number of items in the array.  Let me know if you have a question using this.  I'm glad it worked. :)

bol
0
 
b0lsc0ttCommented:
>> Starting from i<arFolders.length-1 .. etc <<

What do you mean?

bol
0
 
Pravin AsarPrincipal Systems EngineerCommented:
NO Points Please.

Here is a simple HTML to test.

<html>
<body>
<script language="javascript">
// Get current location
url = window.location;
// Extract the pathname of URL , strip the last occurence of '/'

alert (url.pathname.substring(1,url.pathname.lastIndexOf('/')));

</script>
</body>
</html>
0
 
controlthewebAuthor Commented:
Any thoughts on compatibility -- IE 5, anyone?
0
 
b0lsc0ttCommented:
The script I provided is completely cross browser compatible.  Even older browsers like IE 5 should support it.  There is nothing new or unique in the code. :)

Let me know if you have any other questions for this.  You never did clarify the part I asked about but maybe that doesn't matter anymore.

bol
0
 
controlthewebAuthor Commented:
0
 
controlthewebAuthor Commented:
Apologies, bol - I just meant I was leaving off the filename and only writing out the folder names.

Thanks for the method to extract folder names and write into an array.

Now I'm searching for a case or if/then method to create the correct number of URLs and give each the correct tree depth .. /1/2/3/etc. Posted a question for that here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22742178.html
0
 
b0lsc0ttCommented:
I'll take a look at both and post if I can help.  I'm glad I could help with this.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now