使用javascript获取页面网址 [英] Get page url using javascript

查看:113
本文介绍了使用javascript获取页面网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以推荐一种使用JavaScript从网址获取网页名称的方法吗?

Could someone recommend a way to get page name from a url using JavaScript?

例如,如果我有:

http://www.cnn.com/news/1234/news.html?a=1&b=2&c=3

我只需要获取news.html字符串

I just need to get "news.html" string

谢谢!

推荐答案

您可以通过 window.location.pathname 解析非常轻松:

You can do this pretty easily via window.location.pathname parsing:

var file, n;

file = window.location.pathname;
n = file.lastIndexOf('/');
if (n >= 0) {
    file = file.substring(n + 1);
}
alert(file);

...或正如其他人所说,你可以用一行正则表达式来完成。一个看起来很密集的线,但上面有一个评论,应该是一个很好的方式。

...or as others have said, you can do it with a regexp in one line. One kinda dense-looking line, but with a comment above it, should be a good way to go.

这篇关于使用javascript获取页面网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆