Solved

javascript document root relative

Posted on 2003-10-31
10
1,519 Views
Last Modified: 2012-06-27
I have a javascript menu that I would like to use only once across the whole site.  My site looks like this:

main
|--folder1
|--folder2
    |--foldera
|--folder3
|--etc.

So the links in my script are like:
folder1/file.html
folder2/file1.html
folder2/foldera/file3.html
folder3/file4.html

I would like to have the javascript file reside on the highest level but when I do the links wont work on the lower levels.  This is all about the document's relative path.  Is there anyway to solve this so that the menu link (from the script file) in folder2/foldera/file3.html would point back to ../../folder1/file.html?

make sense?
0
Comment
Question by:srobia
  • 5
  • 3
  • 2
10 Comments
 
LVL 10

Expert Comment

by:NetGroove
ID: 9659036
Put your script relative to server root.
0
 
LVL 28

Expert Comment

by:sybe
ID: 9659088
code your links from the root:

/folder1/file.html
/folder2/file1.html
/folder2/foldera/file3.html
/folder3/file4.html


0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9659091
Also look whether <base> tag is of help to you:
http://www.w3schools.com/tags/tag_base.asp


0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 28

Expert Comment

by:sybe
ID: 9659140
there's a problem with IE (well it's a matter of taste probably) that IE interpretes links to a certain frame relative to the current location of the document in the frame. Even if the links are clicked from another frame. Netscape/Mozilla takes link relative to the location of the document where the click is being done. It's been this way forever, and i don't know why they never get on speaking terms about this.

solutions:
- make sure that every link is defined from the root (start with "/")
- use the <base> tag (as NetGroove says)

maybe some other depending on the situation
0
 

Author Comment

by:srobia
ID: 9659757
Sorry none of those really help out.  My javascript file generates the menu.  I can link to the javascript files just fine.  BUT it is the menu that is generated by the javascript that needs to change with the document location.  Is there anyway to do this within javascript?  Basically, find the current document's location then traverse back to the server root and generate a dynamic menu that will automatically make all the links valid.
0
 
LVL 10

Accepted Solution

by:
NetGroove earned 125 total points
ID: 9659962
Your script can read the actual window location and document location.
According to actual location your menu can adapt, for example by comparing to a common base for script and for the page using the script.

0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9659981
Sorry, I just noticed that I repeated your last sentence.
Now when you know that actual window URL is readable by your script, then what is your question???

0
 

Author Comment

by:srobia
ID: 9660014
How do you make the script use the current location to dynamically build the menu?  based upon document.location I was thinking you could parse the string and find the "/" and build a dynamic ../  make sense?
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9660175
Of course make sense. But what is your question?
You can make it any way around.
Decide what you want and I can show you how to do.

If your site is partially online or if your sources are les then let say three hundred lines, then post it here.
0
 

Author Comment

by:srobia
ID: 9667353
I think I figured it out.  I found enough documentation out there to make it a dynamic menu.  thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace link with jQuery 4 20
alert(innerHTML); 8 32
Html Table looping 4 25
Javascript - Uncaught SyntaxError: Unexpected token } (Image Swap onclick) 12 51
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

809 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