473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help Creating JS Relative Path Function

I'm trying to write a Javascript to set a relative path for my files, s
I don't have to type in an absolute path. I know that I need to someho
define the root directory, likely using /, then I need to have th
function check to see where the file sits, then add the appropriat
../ infront of it.

Unfortunatly I'm totally stumped as to what I need to do. A
ex-co-worker of mine used to have a function similar to this, howeve
we've lost touch.

Anyone have an idea how this might work? It makes it so much easier t
place items into webpages without having to hardcode the entire pat
for everything.

Regards,

John William
-
b1azes

Jul 23 '05 #1
3 7277
function getPath( ){
var path = document.location;
var str = new String(path);
var len = str.length;
var start = str.lastIndexOf("/");
var document_path = str.substring(start +1,len);
alert(document_path);
}
Jul 23 '05 #2
b1azesk wrote:
I'm trying to write a Javascript to set a relative path for my files, so
I don't have to type in an absolute path. I know that I need to somehow
define the root directory, likely using /, then I need to have the
function check to see where the file sits, then add the appropriate
./ infront of it.

Unfortunatly I'm totally stumped as to what I need to do. An
ex-co-worker of mine used to have a function similar to this, however
we've lost touch.

Anyone have an idea how this might work? It makes it so much easier to
place items into webpages without having to hardcode the entire path
for everything.

Regards,

John Williams
--
b1azesk


var loc=location.toString()
var fileName = loc.substring(loc.lastIndexOf("/")+1)

Mick
Jul 23 '05 #3
Ivo
"b1azesk" wrote
I'm trying to write a Javascript to set a relative path for my files, so
I don't have to type in an absolute path. I know that I need to somehow
define the root directory, likely using /, then I need to have the
function check to see where the file sits, then add the appropriate
./ infront of it.


Did you know that, because it starts with a slash, <a href="/some/dir/">
always points to same location, no matter how deep in your site you put this
link? No javascript needed at all!

Apart from that:

document.location.pathname.split('/').length-2

is a number representing the number of subdirectories. So

var path='';
for( var i=0; i<document.location.pathname.split('/').length-2; i++)
path+='../';

will give you a string which can be used to back up to the root. Not sure if
this is what you were looking for.
HTH
Ivo
Jul 23 '05 #4

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

Similar topics

4
by: Joe Cybernet | last post by:
Is there any function for combining an absolute and a relative URL to result in an absolute URL? Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to...
7
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...
2
by: peter | last post by:
Hello all, I've read all the posts concerning relative imports of modules and I am glad I saw some consensus on a syntax. At this moment I am however bound to python 2.3. Does anyone know a...
1
by: Flix | last post by:
The following piece of code should turn the folder IN and its subfolders into a single tar file located in the folder OUT: using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.Tar; ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: crisscross27 | last post by:
Hi, I found a page called "myflashfetish" where you chan choose mp3 players for my space, well the problem is this, I wanted to place 2 or more players in myspace in a particular place, I read...
6
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php...
0
by: Stef Mientki | last post by:
hello, I'm running Python 2.5 and want my programs to run at least under Windows and Linux (preferable also Mac). So I guess I should always use relative paths. From most modules I can call a...
0
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.