JS如何获取URL

decher 2010-01-30 01:01:48
需要获取URL最后一段
比如www.abc.com/sports/def.aspx
JS里如何获取def 这个内容?
...全文
557 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
decher 2010-02-01
  • 打赏
  • 举报
回复
<script>
var path= document.location.href;
path=path.substring(path.lastIndexOf("/")+1,path.lastIndexOf("."));
alert(path);
</script>

我测试后这么写就可以获取到最后一节的目录名了。location.href获取的才是string类型,后面再substring才不会出错。

如果像我这个简单的地址的话用字符串操作比正则表达式效率高点。但是包含了额外URL地址还是要用正则表达式了。
decher 2010-02-01
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 ltnrain 的回复:]
楼主请问下你的网站的URL会不会有下面这些类型的

www.abc.com/sports/def.aspx?path=sports/def.aspx
[/Quote]
应该是没这么深层的,只有2层左右的结构很简单的站点
tntooo 2010-01-31
  • 打赏
  • 举报
回复
学习~其实我还想知道
<script>
var path="www.abc.com/sports/def.aspx";
path=path.substring(path.lastIndexOf("/")+1,path.lastIndexOf("."));
alert(path);
</script>
这样和正则的效率哪个高一些!
草根醉秋意 2010-01-31
  • 打赏
  • 举报
回复
楼主请问下你的网站的URL会不会有下面这些类型的

www.abc.com/sports/def.aspx?path=sports/def.aspx
dandande 2010-01-31
  • 打赏
  • 举报
回复
学习
decher 2010-01-31
  • 打赏
  • 举报
回复
感謝回答過問題的大大@@。shan1119的答復就是我需要的了結貼
shan1119 2010-01-30
  • 打赏
  • 举报
回复
<script>
var path="www.abc.com/sports/def.aspx";
path=path.substring(path.lastIndexOf("/")+1,path.lastIndexOf("."));
alert(path);
</script>
超维电脑科技 2010-01-30
  • 打赏
  • 举报
回复
学习
浴火_凤凰 2010-01-30
  • 打赏
  • 举报
回复
正则表达式
wcwtitxu 2010-01-30
  • 打赏
  • 举报
回复


var fileName = document.location.pathname.replace(/.*\/|\.[^.]*$/g,'');

alert(fileName);
decher 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 tntooo 的回复:]
var url=document.location.href?
[/Quote]

提示'match(...).1'为空或不是对象
tntooo 2010-01-30
  • 打赏
  • 举报
回复
var url=document.location.href?
htrxht 2010-01-30
  • 打赏
  • 举报
回复
参考:
http://school.itzcn.com/special-spid-28.html
上面讲解的比较详细,希望对楼主有所帮助。
decher 2010-01-30
  • 打赏
  • 举报
回复
var url=document.location;
url = "www.abc.com/sports/def.aspx";

url直接赋值是可以得到def...但是去掉url = "www.abc.com/sports/def.aspx"之后,直接用url=document.location;就会提示错误了。

但var url=document.location; 这个本身已经获得了URL地址了,为何直接用这个就出错呢?
tntooo 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ltnrain 的回复:]
JScript codevar url=document.location;
url="www.abc.com/sports/def.aspx";var r=/\/(\w+)\.aspx/;
alert(url.match(r)[1]);
alert(r.exec(url)[1]);
[/Quote]

学习了!~ 放IE的script中运行通过!
decher 2010-01-30
  • 打赏
  • 举报
回复
var url=document.location.pathname.replace(/.*\//,'');
var r = /\/(\w+)\.aspx/;
alert(url.match(r)[1]);
alert(r.exec(url)[1]);

我把他放在aspx页面中运行时提示'match(...).1'为空或不是对象
decher 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ltnrain 的回复:]
JScript codevar url=document.location;
url="www.abc.com/sports/def.aspx";var r=/\/(\w+)\.aspx/;
alert(url.match(r)[1]);
alert(r.exec(url)[1]);
[/Quote]

提示缺少对象和语法错误
草根醉秋意 2010-01-30
  • 打赏
  • 举报
回复

var url=document.location;
url = "www.abc.com/sports/def.aspx";
var r = /\/(\w+)\.aspx/;
alert(url.match(r)[1]);
alert(r.exec(url)[1]);

decher 2010-01-30
  • 打赏
  • 举报
回复
需要获取URL最后一段
比如www.abc.com/sports/def.aspx
JS里如何获取def 这个内容?

其实我就只要def这个内容。URL我是能获取到的,不知道怎么操作这个字符串
sohighthesky 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sohighthesky 的回复:]

[/Quote]
改下:
alert(document.location.pathname.replace(/.*\//,''))
加载更多回复(5)

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧