PDA

View Full Version : File's path without filename?


Peeps
02-04-2003, 04:43 PM
I had a quick search for this before I posted but nothing came up.

I've been trying to get the pathname of a file without the filename so instead of
http://www.clanware.co.uk/1.3/1.3/forum.asp
I want
http://www.clanware.co.uk/1.3/1.3/
I've tried

alert (location.protocol + "//" + location.hostname + location.pathname)
and
alert (document.URL)
and
alert (window.location)

But they just give it WITH the filename?

This is probably a very easy question, just I can't find the answer to it *pokes JavaScript Bible*.

Any help appreciated! Thanks!

beetle
02-04-2003, 04:47 PM
I do believe this would work

var path = location.href.match( /^(http.+?\/)/ );

Peeps
02-04-2003, 05:09 PM
Originally posted by beetle
I do believe this would work

var path = location.href.match( /^(http.+?\/)/ );
Rather bizarrely that gives me
http:/,http:/

Any idea why?

beetle
02-04-2003, 05:15 PM
Sorry about that, here's the tested version :D

var path = location.href.match( /^(http.+\/)[^\/]+$/ )[1];

Peeps
02-04-2003, 05:27 PM
Originally posted by beetle
Sorry about that, here's the tested version :D

var path = location.href.match( /^(http.+\/)[^\/]+$/ )[1];
Wahey! Excellent!
Thanks very much! :D

If you're absolutely oozing with time and you're extremely bored, it would be truely fantastic if you could give me a breakdown of whats going on there :) (I don't expect you to, just I like knowing how my code works ;))

Thanks again!

beetle
02-04-2003, 05:48 PM
Well, I can explain it a little bit. Regular expressions are something that you don't just pick up in one or two lessons, but here's the jist of what is going on.

match() is a string method and location.href is a string, so we can apply match() to it.

math() takes a pattern (read: regular expression) as it's argument. It uses that pattern to find matches in the designated string. If found, it returns an array of matches.

Let's look at the pattern. Patterns are delimited by forward slashes, much the same way that strings are delimited by quotes (single or double). So, everthig between the two / / is the pattern

Here's the whole pattern
^(http.+\/)[^\/]+$

^ Indicates that the following match must be at the beginning of the input string.
( opens a remembered match, the start of what will be returned
http match the letters 'http'
.+ match any character in a quantity of 1 or more (the + is the quantifier meaning '1 or more'
\/ Match a foward slash. Since foward slashes delimit patterns, this has to be escaped by a backslash
) Close the remembered match
[^\/]+ Match 1 or more characters that are not foward slashes
$ the previous match must be at the end of the string.

Whoo. Now, since the match returns an ARRAY of results, and the 0 index is occupied by the full matchh (ingnoring the remembered match we made), we want index 1, hence the [1].

For more on regex, go here (http://www.codingforums.com/showthread.php?s=&threadid=14071)

Peeps
02-04-2003, 06:23 PM
Wow, that's excellent mate!
Thanks very much!
*gives medal of forum support excellence*

beetle
02-04-2003, 06:27 PM
Originally posted by Peeps
*gives medal of forum support excellence* "I wonder if I can hock this sucker, I need some cash."

j/k :p

beetle
02-04-2003, 06:32 PM
Oh, I forgot, a color coded version

The pattern
/^(http.+\/)[^\/]+$/

The match
http://www.clanware.co.uk/1.3/1.3/forum.asp

The result - remember, it's an array
[ 'http://www.clanware.co.uk/1.3/1.3/forum.asp', 'http://www.clanware.co.uk/1.3/1.3/' ]

Peeps
02-04-2003, 08:51 PM
Originally posted by beetle
Oh, I forgot, a color coded version

The pattern
/^(http.+\/)[^\/]+$/

The match
http://www.clanware.co.uk/1.3/1.3/forum.asp

The result - remember, it's an array
[ 'http://www.clanware.co.uk/1.3/1.3/forum.asp', 'http://www.clanware.co.uk/1.3/1.3/' ]

Cheers again!
And I'm not a sucker just I used to spend loads of time on a tech support forum and I know how little thanks most people give. ;)

beetle
02-04-2003, 09:09 PM
Hehe, I wasn't calling you a sucker, I was speaking of my "medal" :p

Peeps
02-04-2003, 09:19 PM
Originally posted by beetle
Hehe, I wasn't calling you a sucker, I was speaking of my "medal" :p
Heh