How can I get my Path?

K

Keith Smith

Is there a javascript variable that contains the path of the file from which
it ran?

Something like this....?
alert('My path=' + MyURLPath);

Is this possible?
 
L

Lee

Keith Smith said:
Is there a javascript variable that contains the path of the file from which
it ran?

Something like this....?
alert('My path=' + MyURLPath);

alert(location.href);


Some code may be loaded into the page from other URL's, but
Everything in a web page "runs" from the URL in location.href.
 
L

Lee

Keith Smith said:
Thanks for the solution. I noticed that the above method gives me the URL
path with the file name at the end. For example, it may give me something
like http://www.domain.com/pictures/file1.html.

Is there a way to have it give me the URL and path without the file? For
example:
http://www.domain.com/pictures/?

Since location.href is a string, you can use the String methods.
http://docs.sun.com/source/816-6408-10/string.htm

The lastIndexOf(target) method returns the index of the last occurance
of <target> within the string.

The substring(first,last) returns the <first> through <last> characters
of the string.

Combining these:

var myURL = location.href.substring(0,location.href.lastIndexOf("/")+1);
 
R

RobG

Lee said:
Keith Smith said:

Since location.href is a string, you can use the String methods.
http://docs.sun.com/source/816-6408-10/string.htm

The lastIndexOf(target) method returns the index of the last occurance
of <target> within the string.

The substring(first,last) returns the <first> through <last> characters
of the string.

Combining these:

var myURL =
location.href.substring(0,location.href.lastIndexOf("/")+1);

or

var myURL = location.href.match(/^.*\//);

:p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top