Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does my JS script double up on path names?

Status
Not open for further replies.

MikeCou

Technical User
Apr 23, 2009
10
I use a script to run a nav bar. Everything works fine if I have all the files involved in the root folder. When I create subfolders to group same file types and change the script to show the subfolder name in the path, any further selections will show the subfolder path of the previous selection. Here is a snip of the code:
*************
function LoadNav() {
document.write("<div class='navItem2'><a href='./index.html'>Home</a></div>");
document.write("<div class='navItem2'><a href='./meetings.html'>Meetings</a></div>");

document.write( "<ul><li class='folder'>");
document.write( "<div class='navItem2'><div class='expand'><a href='#'>Show 'n Tell</a></div></div>");
document.write( "<ul>");
document.write("<li><a href='./show_tell/0209.html'>February 2009</a></li>");
document.write("<li><a href='./show_tell/0309.html'>March 2009</a></li>");
document.write("<li><a href='./show_tell/0409.html'>April 2009</a></li>");
document.write( "</ul>");
document.write("</li></ul>");


document.write("<div class='navItem2'><a href='./Members.html'>Members Directory</a></div>");
document.write("<div class='navItem2'><a href='./events.html'>Special &amp; Upcoming Events</a></div>");
}
**********************
The first 2 document.writes point to 2 files in the root folder. The next 3 to files in sub-folder "show_tell". The last 2 are files in the root folder as well.

If I select any of the 4 files that are in the root folder (the first 2 or last 2), all goes well and I can jump between the 4 with no problems... until I select 1 of the files in a subfolder such as "./show_tell/0209.html" for example. The first time, I get the page fine. From then on though, any selection I make, whether the file is in a root folder or not, will show a path of "./show_tell/filename.html". If I select another of the files in the show_tell folder, the path will become "./show_tell/show_tell/0309.html". Which of course is wrong.

In summary, once I have selected to view even 1 page which resides in a subfolder, nothing else will work. I have asked around for help and have tried changing the subfolder path name to ./, ../ and just / and then no / at all. Same results except that the only one that will show me at least the first page is ./ and no other.

Right now I am using a work around using the full path names for each file using the url name and all, but this is not very elegant and it's quite time consuming.

Why does the script double up on the path name? And how can I prevent this? (or can I?)

Thanks.

Mike
 
I personally never use relative paths unless they are absolutley 100% necessary (and I don't know of many instances of where they are)

Using an absolute path allows for more flexibility when moving files around, as well code re-use...

If I have a directory structure as follows:

Root
- /includes/
- /css/
- /scripts/
- /imgages/
- /about/
- /contact/
- index.html

and have a page in the /contact/ folder referencing a css file, instead of:

<link rel="stylesheet" href="../includes/css/default.css" />

use

<link rel="stylesheet" href="/includes/css/default.css" />

even if I have a subfolder within contact and want to reference an image in it:

<img src="/contact/images/myImage.jpg" />

^ go back to the root and filter through the folders to get to the file. If I ever decide to re-use that line of code ANYWHERE in my directory structure (no matter how big it gets), it will ALWAYS work.


The single dot-slash combination is telling your script to look for a folder in the current directory - if your javascript is in a separate file it could be looking there, it could also be looking in the directory from where the js file is referenced.


TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
I personally never use relative paths unless they are absolutley 100% necessary (and I don't know of many instances of where they are)

Using an absolute path allows for more flexibility when moving files around, as well code re-use...

I couldn't disagree more. Surely using relative paths gives more flexibility for moving whole directory structures around?

Personally, I will always use relative paths if I can.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I couldn't disagree more. Surely using relative paths gives more flexibility for moving whole directory structures around?

What exactly does that mean?

If I have a page in this location:

/public/contact/index.html

and my stylesheet is located here

/includes/css/default.css

my absolute reference is this:

<link rel="stylesheet" tye="text/css" href="/includes/css/default.css" />

my relative reference is this:

<link rel="stylesheet" tye="text/css" href="../../includes/css/default.css" />

now if I create a sub directory within "contact" and have a page like this:

/public/contact/north_america/index.html

I CANNOT use the previous relative reference without changing it, I can, however use the exact same absolute reference in that new directory and anywhere else.

Another example..if I need to create a new version but want to keep the "public" directory and now have:

/v2/public/contact/index.html or
/public/v2/contact/index.html

The absolute reference to the stylesheet won't break, but the relative one will....

Maybe I'm missing something - please elaborate on how relative paths make more sense...








TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Another thing - maybe I'm not using the same terminology as you use..but I consider a "ROOT RELATIVE" path an "ABSOLUTE" path (I guess I could call it the "ABSOUTE ROOT" path)

A relative path (in my terms) is when you reference something relative from exactly where you are (i.e. down a directory, down another,then up one)

I would never use as a starting point (which may be what you (and others) consider to be true absolute)

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Thanks for the suggestions but probably due to my "newbyness", I still can't understand why the script is doubling up? Is there some way to tell js that once you've viewed a page, reset the path to the default (or a default)? The information passed on to the script now is obviously held in a cache somewhere. Can we have that dumped and renewed every time a new selection is made?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top