我们如何修剪网址的一部分? [英] How do we trim a part of URL?
问题描述
嗨!我们如何修剪或删除部分URL或路径?
例如。 http://www.mywebsite/folder/somefile.htm --- - 我想修剪
http://www.mywebsite/folder/ 所以我剩下的就是文件名。
< script language =" JavaScript">
var pathtotrim = location。 href;
document.write(pathtotrim);
< / script>
Paul
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
推荐答案
paul写道:
< script language =" JavaScript">
不推荐使用,请改用以下内容:
< script type =" text / javascript">
var pathtotrim = location.href;
document.write(pathtotrim);
<script language="JavaScript">
Deprecated, use the following instead:
<script type="text/javascript">
var pathtotrim = location.href;
document.write (pathtotrim);
这样的事情应该这样做:
var path = location。 pathname.split(''/'');
path.pop();
alert(location.protocol +''//''+ location.host + path.join(''/'')+''/'');
JW
Something like this should do it:
var path = location.pathname.split(''/'');
path.pop();
alert(location.protocol + ''//'' + location.host + path.join(''/'') + ''/'');
JW
paul写道:
paul wrote:
嗨!我们如何删除或删除部分URL或路径?
例如。 http://www.mywebsite/folder/somefile.htm --- - 我想修剪
http://www.mywebsite/folder/ 所以我剩下的就是文件名。
< script language =" JavaScript">
var pathtotrim = location.href;
文件。写(pathtotrim);
< / script>
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>
请注意,由于跨平台的不同* local * path语法
和浏览器,此解决方案可能会因Mac而失败肯定是
IE / Win本地文件。
然而它总是适用于URL'。
For一个真的通用解决方案然后应该使用RegExp,这是我b $ b不强的。
<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.
HI!非常感谢所有的信息,我试着看看哪一个在我的
情况下效果最好。 :)
Paul
" paul" < PA *********** @ sympatico.ca>在消息中写道
新闻:J2 *************** @ news20.bellglobal.com ...
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...
嗨!我们如何删除或删除部分URL或路径?
例如。 http://www.mywebsite/folder/somefile.htm --- - 我想修剪
http://www.mywebsite/folder/ 所以我剩下的就是文件名。
< script language =" JavaScript">
var pathtotrim = location.href;
文件。写(pathtotrim);
< / script>
保罗
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
这篇关于我们如何修剪网址的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!