469,362 Members | 2,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

How do we trim a part of URL?

Hi! How do we trim out or remove a part of a URL or path?

eg. http://www.mywebsite/folder/somefile.htm ----I want to trim
http://www.mywebsite/folder/ so All I have left is the file name.

<script language="JavaScript">

var pathtotrim = location.href;
document.write (pathtotrim);

</script>

Paul
Feb 26 '06 #1
10 4993
paul wrote:
<script language="JavaScript">

Deprecated, use the following instead:

<script type="text/javascript">
var pathtotrim = location.href;
document.write (pathtotrim);


Something like this should do it:

var path = location.pathname.split('/');
path.pop();
alert(location.protocol + '//' + location.host + path.join('/') + '/');
JW
Feb 26 '06 #2
VK

paul wrote:
Hi! How do we trim out or remove a part of a URL or path?

eg. http://www.mywebsite/folder/somefile.htm ----I want to trim
http://www.mywebsite/folder/ so All I have left is the file name.

<script language="JavaScript">

var pathtotrim = location.href;
document.write (pathtotrim);

</script>


<script type="text/javascript">
var pathfortrim = self.location.pathname;
alert(pathfortrim.substring(pathfortrim.lastIndexO f('/')+1));
</script>

Please note that due to different *local* path syntax across platforms
and browsers this solution will possibly fail for Mac and for sure for
IE/Win for local files.
Nevertheless it will always work for URL's.

For a really universal solution a RegExp should be used then, which I'm
not strong at.

Feb 26 '06 #3
HI! Thanks a lot for all the info, I try to see which one works best in my
situation. :)

Paul

"paul" <pa***********@sympatico.ca> wrote in message
news:J2***************@news20.bellglobal.com...
Hi! How do we trim out or remove a part of a URL or path?

eg. http://www.mywebsite/folder/somefile.htm ----I want to trim
http://www.mywebsite/folder/ so All I have left is the file name.

<script language="JavaScript">

var pathtotrim = location.href;
document.write (pathtotrim);

</script>

Paul

Feb 26 '06 #4
kay
VK - if u want really good regular expressions tutorial, i found one

http://www.regular-expressions.info/quickstart.html

i thought i will never leran to use regexp, but this site explained it
all to me :-)

Feb 26 '06 #5
JRS: In article <11**********************@u72g2000cwu.googlegroups .com>
, dated Sun, 26 Feb 2006 06:35:06 remote, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.com> posted :

paul wrote:
Hi! How do we trim out or remove a part of a URL or path?

eg. http://www.mywebsite/folder/somefile.htm ----I want to trim
http://www.mywebsite/folder/ so All I have left is the file name.

For a really universal solution a RegExp should be used then, which I'm
not strong at.


Since you know you do not have a good answer, you need not have
responded.

St = "http://www.mywebsite/folder/somefile.htm"
St = St.replace(/.*\/(.*)/, "$1")
// returns all after the last /
or St = St.match(/.*\/(.*)/, "$1")[1]

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 27 '06 #6
VK wrote:
paul wrote:
<script language="JavaScript">
The `language' attribute is deprecated in HTML 4, the `type' attribute is
required:

<script type="text/javascript">
var pathtotrim = location.href;
document.write (pathtotrim);

</script>


<script type="text/javascript">
var pathfortrim = self.location.pathname;


`self' is a property of `window' or a host-defined property of the Global
Object. Since it refers to either object and either object also has a
`location' property, using the `self' property is completely unnecessary
here, probably error-prone since it introduces another dependency on a
host-defined property, and probably unnecessary always.
PointedEars
Feb 28 '06 #7
Dr John Stockton wrote:
[...] VK <sc**********@yahoo.com> posted :
paul wrote:
Hi! How do we trim out or remove a part of a URL or path?

eg. http://www.mywebsite/folder/somefile.htm ----I want to trim
http://www.mywebsite/folder/ so All I have left is the file name.


For a really universal solution a RegExp should be used then, which I'm
not strong at.


Since you know you do not have a good answer, you need not have
responded.

St = "http://www.mywebsite/folder/somefile.htm"
St = St.replace(/.*\/(.*)/, "$1")
// returns all after the last /
or St = St.match(/.*\/(.*)/, "$1")[1]


Since `.' (any character except newline) also matches `/', more efficient is

St = St.match(/([^\/]*)$/)[0];
PointedEars
Feb 28 '06 #8
Thomas 'PointedEars' Lahn wrote:
Dr John Stockton wrote:
St = "http://www.mywebsite/folder/somefile.htm"
St = St.replace(/.*\/(.*)/, "$1")
// returns all after the last /
or St = St.match(/.*\/(.*)/, "$1")[1]


Since `.' (any character except newline) also matches `/', more efficient
is

St = St.match(/([^\/]*)$/)[0];


And slightly more efficient is

St = St.match(/[^\/]*$/)[0];

The backreference is not used, and so the capturing parentheses are
unnecessary here.
PointedEars
Feb 28 '06 #9
JRS: In article <15****************@PointedEars.de>, dated Tue, 28 Feb
2006 04:36:37 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
Dr John Stockton wrote:

St = "http://www.mywebsite/folder/somefile.htm"
St = St.replace(/.*\/(.*)/, "$1")
// returns all after the last /
or St = St.match(/.*\/(.*)/, "$1")[1]


Since `.' (any character except newline) also matches `/', more efficient is

St = St.match(/([^\/]*)$/)[0];


Now you're answering with excessive haste again, once more showing your
juvenile attitude and thereby missing an opportunity to observe that in
the last line which you quoted the , "$1" should have been absent -
though harmless, as it was not found on test.

And if you had been less hasty, of course, you might not have felt a
need to follow-up to your own first response. You're getting rather
careless, especially when you're in a bad mood, and should be asleep.

Just look and see who in this newsgroup most frequently corrects his own
answers.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 28 '06 #10
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
Dr John Stockton wrote:
St = "http://www.mywebsite/folder/somefile.htm"
St = St.replace(/.*\/(.*)/, "$1")
// returns all after the last /
or St = St.match(/.*\/(.*)/, "$1")[1]


Since `.' (any character except newline) also matches `/', more efficient
is

St = St.match(/([^\/]*)$/)[0];


Now you're answering with excessive haste again, once more showing your
juvenile attitude and thereby missing an opportunity to observe that in
the last line which you quoted the , "$1" should have been absent -
though harmless, as it was not found on test.

And if you had been less hasty, of course, you might not have felt a
need to follow-up to your own first response. You're getting rather
careless, especially when you're in a bad mood, and should be asleep.


In other words, you posted something that is unnecessary and could be
considered nonsense, but, in contrast to me, you are far too arrogant
to admit that. Instead you are blaming others for not seeing all your
errors. And you try to sweep your error under the rug by performing
an ad hominem attack against the person who pointed out the problem,
and a more efficient solution than yours.

So far for juvenile attitudes.
PointedEars
Mar 1 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by DrBob | last post: by
22 posts views Thread by Simon | last post: by
3 posts views Thread by mhk | last post: by
22 posts views Thread by Terry Olsen | last post: by
5 posts views Thread by brett | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.