473,795 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Relative paths in .js file

I've got a javascript menu that uses an external .js file. In the .js file,
there is a function that gets called by each of my .html pages to build up
the menu for the page. The way I want to structure my site is to put my
main index.html file in the root folder, and then have subdirectories for
various logical sections of my site that will contain related .html files.
I was hoping that the paths in the .js file would be relative to the
directory the .js file was located in (as CSS files appear to do), but that
doesn't appear to be the case. It appears the relative path is relative to
the directory the html file is in.

So, the problem is if I make the relative paths, in the .js file, work for
the main index.html file, it doesn't work for any of the .html files in the
subdirectories, or if I get to work for the files in the subdirectories, it
doesn't work in the main index.html file. I'm just looking for a best
practice way to solve this problem. One way I thought about doing it was to
put a parameter into the menu-building function that would indicate if the
calling html "file" was in the root directory or one of the subdirectories.
I also realized that I could put the main index.html into a subfolder itself
and that would solve the problem, but then I believe I'd have to put some
default html file in my root and redirect to the "real" default page for the
site. I'd like to avoid that option.

Any ideas would be much appreciated.

Thanks!
Jul 23 '05 #1
3 2678
epigram ha scritto:
One way I thought about doing it was to
put a parameter into the menu-building function that would indicate if the
calling html "file" was in the root directory or one of the subdirectories.


I use this solution, too

Bye
Jul 23 '05 #2
Il Mon, 27 Jun 2005 11:03:18 -0400, epigram ha scritto:
I've got a javascript menu that uses an external .js file. So, the problem is if I make the relative paths, in the .js file, work for
the main index.html file, it doesn't work for any of the .html files in the
subdirectories, or if I get to work for the files in the subdirectories, it
doesn't work in the main index.html file. I'm just looking for a best
practice way to solve this problem. Any ideas would be much appreciated.


in the external .js file refer to location.pathna me and act consequently

--
Firefox, il browser sicuro, veloce, affidabile.
<http://snipurl.com/ftpi>
Jul 23 '05 #3
If you reference all your src and href urls from the root (with a leading
slash) they will link correctly.
For instance:
If you want to reference 'http://www.yourhost.co m/shared/somefile.gif' from
the root, from the "/shared/" directory or from deep in some other
directory, use:
" src='/shared/somefile.gif' ".
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2779
by: Doug | last post by:
If I were to write an include with a relative path like include("../conf/config.php"); What is the use? As far as I understand it, the path is relative to the first script that is called by php. In other words, if the current working directory is /www/ and you were
7
2837
by: mark | last post by:
I am designing a website at the moment and looking at the difference between relative and absolute url links which is driving me crazy! I would like to use relative paths, but it is proving very restrictive as to how I design the file structure when it comes to including files. I currently have something like below: folder1 folder2 images ---image1.gif
5
4113
by: jason | last post by:
Can anyone help me find a solution to quickly working out relative paths to a folder in the root of my server... Although it easy when you go - say - two levels down: .../includes it becomes increasingly difficult to ball park the folder when you go deeper and wider down:
7
78647
by: Rizaan Jappie | last post by:
is it possible to get the relative path based on a absolute path in c#? *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
2
1855
by: Joe | last post by:
Hi, can someone tell me how to set up relativ paths in VS2003 C++ ? I have some source with a tree directory structure that segments include files in various directories: #include "../include/config.h" /* Keep this first */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/complex.h" #include "../include/su3.h"
19
5114
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other things, like php code), which resolves the correct image path when opened under / but when x.php is read into a file under /dir the image no longer resolves, for obvious reasons. With absolute paths, this isn't an issue but it is with relative...
8
2604
by: JJ | last post by:
I'm confused about paths. I have a functionn that uses the mappath method, which I think requires a virtual path (is that the same as a relative path?). But this doesn't always work as the path can take the form of the following: 1. /directory/images/xyz.gif (works fine) 2. http://localhost:1234/www.mytestwebsite.com/directory/images/xyz.gif (doesn't work) 3. http:///www.someremotesite.com/directory/images/abc.gif (doesn't work)
2
4304
by: BD | last post by:
Hi there. Using 8.2 on Windows. I have a situation where I have a db backup, which I want to deploy to a group of developer workstations. The target directory for the database files will be consistent, but the location that the backup file is on may vary. I have gone through the entire backup and restore process, and it
2
4311
by: Peted | last post by:
Hi i have a circumstance where a user unzips a file, with a certain layer of directories to get to a textfile. So in any directory on the HDD they may end up with something like dir1/dir2/dir3/dir4/dir5/file.txt
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.