How do we trim a part of URL?

J

Janwillem Borleffs

paul said:
<script language="JavaScript">

Deprecated, use the following instead:

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
 
V

VK

paul said:
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.lastIndexOf('/')+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.
 
P

paul

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

Paul
 
T

Thomas 'PointedEars' Lahn

The `language' attribute is deprecated in HTML 4, the `type' attribute is
required:

<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
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] VK said:
paul said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 28 Feb
2006 04:36:37 remote, seen in Thomas
'PointedEars' Lahn said:
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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted :
Dr said:
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
 

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,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top